2018-03-24, 10:32 PM
First, the bug (1.8.14). The situation: one has uploaded an attachment for a new/edit post and one has selected another file to be uploaded but "Add Attachment to upload the second attachment has not been clicked yet. If at that point, one clicks on "Remove Attachment", the selected attachment will be uploaded.
Now an improvement. I was interested in allowing multi-attachments adds. I found a plugin that added that capability, but it also added a number of other features. I found the plugin unusable because of various bugs, many of which were related to features other than supporting multifile upload. I implemented the following fairly straight-forward changes to allow the multi-file uploads. If anyone wants to try it, and provide feedback, at some point I may turn it into a plugin.
Uploaded files come into the *.php code via the variables "$_FILES['attachment'][$field]", where $field has various values like 'name' and 'size' for various aspects of the uploaded file. For multi-file uploads, those variables become arrays instead of singular values. Where those variables are used, the following code segment must be inserted.
Step 1: The affected files are newthread.php, editpost.php and newreply.php, around line 225, give or take, in each. In each file, insert the code before the first "if" statements that process the attachment (and add another closing curly brace for the added "foreach" statement).
Step 2: In that "if" staement, chnage all references to "$_FILES" to "$FILE".
Example: in newthread.php, the new code segment would look like this:
[attachment=40094]
Step 4: Change the "input" tag in template "post_attachments_new" to have name "attachment[]" (braces added) and the keyword "multiple".
Older browsers may still be limited to single attachment uploads but it still should work for that.
Now an improvement. I was interested in allowing multi-attachments adds. I found a plugin that added that capability, but it also added a number of other features. I found the plugin unusable because of various bugs, many of which were related to features other than supporting multifile upload. I implemented the following fairly straight-forward changes to allow the multi-file uploads. If anyone wants to try it, and provide feedback, at some point I may turn it into a plugin.
Uploaded files come into the *.php code via the variables "$_FILES['attachment'][$field]", where $field has various values like 'name' and 'size' for various aspects of the uploaded file. For multi-file uploads, those variables become arrays instead of singular values. Where those variables are used, the following code segment must be inserted.
$fields = array ('name', 'type', 'tmp_name', 'error', 'size');
if (is_array($_FILES['attachment']['name']))
{
// Already in multi-attachment array format
$attachments = $_FILES;
}
else
{
// Convert original-style non-array $_FILES['attachment'][$field] to array format in $attachments
$attachments = array('attachment' => array());
foreach ($fields as $field)
$attachments['attachment'][$field] = array($_FILES['attachment'][$field]);
}
foreach ($attachments['attachment']['name'] as $key => $name)
{
// Convert array $attachments['attachment'][$field][$key] to non-array format in $FILE
$FILE = array('attachment' => array());
foreach ($fields as $field)
$FILE['attachment'][$field] = $attachments['attachment'][$field][$key];
Step 1: The affected files are newthread.php, editpost.php and newreply.php, around line 225, give or take, in each. In each file, insert the code before the first "if" statements that process the attachment (and add another closing curly brace for the added "foreach" statement).
Step 2: In that "if" staement, chnage all references to "$_FILES" to "$FILE".
Example: in newthread.php, the new code segment would look like this:
[attachment=40094]
Step 4: Change the "input" tag in template "post_attachments_new" to have name "attachment[]" (braces added) and the keyword "multiple".
<input type="file" name="attachment[]" size="30" class="fileupload" multiple/>
Older browsers may still be limited to single attachment uploads but it still should work for that.