Browse Source

Added multiple file select dialog for post form

Mostly 808c2072d3
pull/68/head
discomrade 2 years ago
parent
commit
4d3988bc7c
  1. 18
      inc/functions.php
  2. 44
      post.php
  3. 9
      templates/post_form.html

18
inc/functions.php

@ -2853,6 +2853,24 @@ function link_for($post, $page50 = false, $foreignlink = false, $thread = false)
return sprintf($tpl, $id, $slug);
}
// Generate filename, extension, file id and file and thumb paths of a file
function process_filenames($file, $board_dir, $multiple, $i){
global $config;
$file['filename'] = urldecode($file['name']);
$file['extension'] = strtolower(mb_substr($file['filename'], mb_strrpos($file['filename'], '.') + 1));
if (isset($config['filename_func']))
$file['file_id'] = $config['filename_func']($file);
else
$file['file_id'] = time() . substr(microtime(), 2, 3);
if ($multiple)
$file['file_id'] .= "-$i";
$file['file'] = $board_dir . $config['dir']['img'] . $file['file_id'] . '.' . $file['extension'];
$file['thumb'] = $board_dir . $config['dir']['thumb'] . $file['file_id'] . '.' . ($config['thumb_ext'] ? $config['thumb_ext'] : $file['extension']);
return $file;
}
function prettify_textarea($s){
return str_replace("\t", '	', str_replace("\n", '
', htmlentities($s)));
}

44
post.php

@ -657,6 +657,28 @@ function handle_post(){
}
// Convert multiple upload format to array of files. This makes the following code
// work the same whether we used the JS or HTML multiple file upload techniques.
if (array_key_exists('file_multiple', $_FILES)) {
$file_array = $_FILES['file_multiple'];
$_FILES = [];
// If more than 0 files were uploaded
if (!empty($file_array['tmp_name'][0])) {
$i = 0;
$n = count($file_array['tmp_name']);
while ($i < $n) {
$_FILES[strval($i+1)] = array(
'name' => $file_array['name'][$i],
'tmp_name' => $file_array['tmp_name'][$i],
'type' => $file_array['type'][$i],
'error' => $file_array['error'][$i],
'size' => $file_array['size'][$i]
);
$i++;
}
}
}
$post['name'] = $_POST['name'] != '' ? $_POST['name'] : $config['anonymous'];
$post['subject'] = $_POST['subject'];
$post['email'] = str_replace(' ', '%20', htmlspecialchars($_POST['email']));
@ -781,20 +803,14 @@ function handle_post(){
$i = 0;
foreach ($_FILES as $key => $file) {
if ($file['size'] && $file['tmp_name']) {
$file['filename'] = urldecode($file['name']);
$file['extension'] = strtolower(mb_substr($file['filename'], mb_strrpos($file['filename'], '.') + 1));
if (isset($config['filename_func']))
$file['file_id'] = $config['filename_func']($file);
else
$file['file_id'] = time() . substr(microtime(), 2, 3);
if (sizeof($_FILES) > 1)
$file['file_id'] .= "-$i";
$file['file'] = $board['dir'] . $config['dir']['img'] . $file['file_id'] . '.' . $file['extension'];
$file['thumb'] = $board['dir'] . $config['dir']['thumb'] . $file['file_id'] . '.' . ($config['thumb_ext'] ? $config['thumb_ext'] : $file['extension']);
$post['files'][] = $file;
$i++;
if (!in_array($file['error'], array(UPLOAD_ERR_NO_FILE, UPLOAD_ERR_OK))) {
error(sprintf3($config['error']['phpfileserror'], array(
'index' => $i+1,
'code' => $file['error']
)));
}
$post['files'][] = process_filenames($file, $board['dir'], (sizeof($_FILES) > 1), $i);
$i++;
}
}
}

9
templates/post_form.html

@ -146,10 +146,15 @@
{% trans %}File{% endtrans %}
</th>
<td class="upload-area">
<input type="file" name="file" id="upload_file">
<input type="file" name="file_multiple[]" id="upload_file" multiple/>
{% if 'js/file-selector.js' in config.additional_javascript %}
<script type="text/javascript">if (typeof init_file_selector !== 'undefined') init_file_selector({{ config.max_images }});</script>
<script type="text/javascript">
if (typeof init_file_selector !== 'undefined') {
var iOS_ifs = !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform);
if(!iOS_ifs) { init_file_selector({{ config.max_images }}); }
}
</script>
{% endif %}

Loading…
Cancel
Save