我有一个用于文件附件的php脚本,代码附加了一个文件,然后通过电子邮件将其发送给收件人,我想知道是否有人知道我可以对其进行调整以允许多次上传的方法吗?
我已附上以下代码...
<?php
if($_POST && isset($_FILES['file']))
{
$recepient_email = "me@myemail.com";
$subject = "New File Attached";
$sender_name = filter_var($_POST["s_name"], FILTER_SANITIZE_STRING); //capture sender name
$sender_email = filter_var($_POST["s_email"], FILTER_SANITIZE_STRING); //capture sender email
$sender_message = filter_var($_POST["s_message"], FILTER_SANITIZE_STRING); //capture message
$attachments = $_FILES['file'];
//php validation
if(strlen($sender_name)<1){
echo('<div class="alert alert-danger" role="alert">Name is too short or empty</div>');
}
if (!filter_var($sender_email, FILTER_VALIDATE_EMAIL)) {
echo('<div class="alert alert-danger" role="alert">Invalid email</div>');
}
$file_count = count($attachments['name']); //count total files attached
$boundary = md5("New File");
if($file_count > 0){ //if attachment exists
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$sender_email."\r\n"; //force apache to send email from @tees
$headers .= "Reply-To: ".$sender_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//message text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($sender_message));
//attachments
for ($x = 0; $x < $file_count; $x++){
if(!empty($attachments['name'][$x])){
if($attachments['error'][$x]>0) //exit script and output error if we encounter any
{
$mymsg = array(
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder" );
die($mymsg[$attachments['error'][$x]]);
}
//get file info
$file_name = $attachments['name'][$x];
$file_size = $attachments['size'][$x];
$file_type = $attachments['type'][$x];
//read file
$handle = fopen($attachments['tmp_name'][$x], "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=\"$file_name\"\r\n";
$body .="Content-Disposition: attachment; filename=\"$file_name\"\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
}
}
}else{ //send plain email otherwise
$headers = "From:".$from_email."\r\n".
"Reply-To: ".$sender_email. "\n" .
"X-Mailer: PHP/" . phpversion();
$body = $sender_message;
}
$sentMail = @mail($recepient_email, $subject, $body, $headers);
if($sentMail) //output success or failure messages
{
echo('<div class="alert alert-success" role="alert">
<h4><span class="fa fa-check"></span> Success</h4>
Your filehas now been submitted for approval.<br />
Please allow up to 24 hours somebody to contact you.<br />
<a href="./index" class="alert-link">Upload Another Form</a></div>');
}else{
die('<div class="alert alert-danger" role="alert">Could not send attachments. Please contact the <a href="mailto:me@myemail.com" class="alert-link">Team</a></div>');
}
}
?>
文件附件当前仅允许单个文件附件。
非常感谢您的帮助。