Reputation: 37
Ok bit of a tricky one I think, I have a form that I want to use to allow users to upload a files and when they click submit it emails the file to someone. i have this working for one file.
See code
<html>
<head>
<title>Administration - upload new files</title>
</head>
<body>
<h1>Upload new news files</h1>
<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
Upload this file: <input name="userfile" type="file">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
Upload this file: <input name="userfile" type="file">
<input type="submit" value="Send File">
</form>
</body>
</html>
and
<html>
<head>
<title>Uploading...</title>
</head>
<body>
<h1>Uploading file...</h1>
<?php
$youremail = "[email protected]";
$tmp_name = $_FILES['userfile']['tmp_name'];
$type = $_FILES['userfile']['type'];
$name = $_FILES['userfile']['name'];
$size = $_FILES['userfile']['size'];
if (file_exists($tmp_name))
{
if(is_uploaded_file($tmp_name)) {
$file = fopen($tmp_name,'rb'); //open the file
$data = fread($file,filesize($tmp_name)); //read the file
fclose($file); // close the file
$data = chunk_split(base64_encode($data)); // encode and split
}
$bound_text = "x".md5(mt_rand())."x";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";
$headers = "From: {$sendersname}<{$sendersemail}>\r\n"
."MIME-Version: 1.0\r\n"
."Content-Type: multipart/mixed; boundary=\"$bound_text\"";
$message .= "If you can see this MIME than your client doesn't accept MIME types!\r\n"
.$bound;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
.$sendcontent."\r\n"
.$bound;
$message .= "Content-Type: ".$type."; name=\"".$name."\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-disposition: attachment; file=\"".$name."\"\r\n"
."\r\n"
.$data
.$bound_last;
}
mail($youremail, $subject, $message, $headers);
?>
The problem is coming when I want to attach more than one file. ANd what order do i go about this, on the first page I want it so that when you click on attatch file it will add a new file box and then when the click it again another one appears and so on. So the form is dynamically created.
This will then bring me to the next issue which will be sending the attachments as we wont know how many the user has attached.
Any pointers as im sure its not going to be as easy as changing the if (file_exists($tmp_name) to a while?
Thank you so much in advance
Edit so now i have
<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
Upload this file: <input name="userfile[]" type="file">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
Upload this file: <input name="userfile[]" type="file">
<input type="submit" value="Send File">
and
<html>
<head>
<title>Uploading...</title>
</head>
<body>
<h1>Uploading file...</h1>
<?php
$youremail = "[email protected]";
$i = count($_FILES['userfile']);
foreach($_FILES['userfile'] as $file){
$tmp_name = $file['tmp_name'];
$type = $file['type'];
$name = $file['name'];
$size = $file['size'];
if (file_exists($tmp_name))
{
if(is_uploaded_file($tmp_name)) {
$file = fopen($tmp_name,'rb'); //open the file
$data = fread($file,filesize($tmp_name)); //read the file
fclose($file); // close the file
$data = chunk_split(base64_encode($data)); // encode and split
}
$bound_text = "x".md5(mt_rand())."x";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";
$headers = "From: {$sendersname}<{$sendersemail}>\r\n"
."MIME-Version: 1.0\r\n"
."Content-Type: multipart/mixed; boundary=\"$bound_text\"";
$message .= "If you can see this MIME than your client doesn't accept MIME types!\r\n"
.$bound;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
.$sendcontent."\r\n"
.$bound;
$message .= "Content-Type: ".$type."; name=\"".$name."\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-disposition: attachment; file=\"".$name."\"\r\n"
."\r\n"
.$data
.$bound_last;
}
echo "$i";
}
mail($youremail, $subject, $message, $headers);
?>
It is sending a email but no attachments I put the $i in to see what was going on and its returning as 5 shoundn't it be 2 if there is only 2 file uploads?
Upvotes: 1
Views: 189
Reputation: 6342
You can alter your file input to be an array like this:
<input name="userfile[]" type="file">
This will add an additional depth to your $_FILES
array. You could determine the number of attachments uploaded with count($_FILES['userfile'])
. And loop through the provided attachments with:
foreach($_FILES['userfile'] as $file) {
// Access the elements with:
// $file['name']
// etc..
}
As for clicking a link to create an arbitrary number of file inputs, you would need to use Javascript. This can be done with vanilla JS, but if you are using jQuery you may want to look in to clone().
Upvotes: 2