I have been using a form to email script that includes several attachments. All has worked fine until I tried integrating Javascript dynamic file input that allows the user to create file upload buttons based on need as apposed to having a fixed number of file upload buttons. They keep clicking the "Attach Another File" button until they have as many as they need.
My old file inputs looked like this.
<input type="file" name="apples">
<input type="file" name="oranges">
<input type="file" name="bananas">
Now that I am using the Javascript below, the attachments are no longer included in the emails.
<div class="file_upload" id="f1"><input name="file[]" type="file"/>1</div>
<div id="file_tools">
<img src="images/file_add_icon.jpg" width="50" id="add_file" title="Add new input"/>
<img src="images/file_delete_icon.jpg" width="50" id="del_file" title="Delete"/>
</div>
<script type='text/javascript'>
$(document).ready(function(){
var counter = 2;
$('#del_file').hide();
$('img#add_file').click(function(){
$('#file_tools').before('<div class="file_upload" id="f'+counter+'"><input name="file[]" type="file">'+counter+'</div>');
$('#del_file').fadeIn(0);
counter++;
});
$('img#del_file').click(function(){
if(counter==3){
$('#del_file').hide();
}
counter--;
$('#f'+counter).remove();
});
});
</script>
When I print the arrays from the file upload they look like this.
Array ( [name] => Array ( [0] => test.pdf ) [type] => Array ( [0] => application/pdf ) [tmp_name] => Array ( [0] => /tmp/phpvnDpQJ ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 26260 ) )
This is the file sending script that was working fine when the file names were hard coded. Now that they are dynamic (name="file[]"), it no longer works. This code must need to be changed but I can't figure out how.
foreach($_FILES['file'] as $userfile){
// store the file information to variables for easier access
$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];
echo $tmp_name;
echo $type;
echo $name;
echo $size;
// if the upload succeded, the file will exist
if (file_exists($tmp_name)){
// check to make sure that it is an uploaded file and not a system file
if(is_uploaded_file($tmp_name)){
// open the file for a binary read
$file = fopen($tmp_name,'rb');
// read the file content into a variable
$data = fread($file,filesize($tmp_name));
// close the file
fclose($file);
// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}
// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content.
// NOTE: we don't set another boundary to indicate that the end of the
// file has been reached here. we only want one boundary between each file
// we'll add the final one after the loop finishes.
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n";
}
}
// here's our closing mime boundary that indicates the last of the message
$message.="--{$mime_boundary}--\n";
// now we just send the message
if (@mail($to, $subject, $message, $headers)){
echo 'Success';
}