diff --git a/inc/functions.php b/inc/functions.php index 035d1fcc..ab43210b 100644 --- a/inc/functions.php +++ b/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))); } diff --git a/post.php b/post.php index 3b6ba440..599e9353 100644 --- a/post.php +++ b/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++; } } } diff --git a/templates/post_form.html b/templates/post_form.html index 1d77443a..02c6087f 100644 --- a/templates/post_form.html +++ b/templates/post_form.html @@ -146,10 +146,15 @@ {% trans %}File{% endtrans %} - + {% if 'js/file-selector.js' in config.additional_javascript %} - + {% endif %}