PHP表单文件上传:2个输入,但仅附加1个文件
我想弄明白这一点已经有一段时间了,但似乎还是弄不明白。我有一个一个文件上传的工作代码,工作良好,现在试图创建一个2上传。为了尝试添加多个文件,我添加了一个foreach循环,并将表单输入的name属性切换为name=“photo[]”,以便它读取一个数组。表单提交时没有错误,但我仍然只收到电子邮件附件中的第一个文件。我已经看到很多类似的问题,但仍然无法理解-谢谢你的耐心!(我还玩过:PHP表单文件上传:2个输入,但仅附加1个文件,php,arrays,forms,file,foreach,Php,Arrays,Forms,File,Foreach,我想弄明白这一点已经有一段时间了,但似乎还是弄不明白。我有一个一个文件上传的工作代码,工作良好,现在试图创建一个2上传。为了尝试添加多个文件,我添加了一个foreach循环,并将表单输入的name属性切换为name=“photo[]”,以便它读取一个数组。表单提交时没有错误,但我仍然只收到电子邮件附件中的第一个文件。我已经看到很多类似的问题,但仍然无法理解-谢谢你的耐心!(我还玩过: for($i = 0; $i < 2; $i++) { ($i=0;$i$file){ $temp=ex
for($i = 0; $i < 2; $i++) {
($i=0;$i<2;$i++)的{
代替和以及foreach),但这是我当前的代码
HTML表单:
<form enctype="multipart/form-data" method= "post" action="couple_uploader_test.php">
<input type="hidden" name="myEmail" value="info@website.co.uk">
<input type="hidden" name="subject" value="Couple Portrait Order">
<label>Name:</label><input type="text" name="customerName" required="true"/><br/>
<label>Email:</label><input type="email" name="customerEmail" required="true"/><br/>
<label>Photo:</label><input type="file" name="photo[]" accept="image/*" required="true"/>
<label>2nd Photo (optional):</label><input type="file" name="photo[]" accept="image/*"/>
<label>Notes:</label><textarea name="message" cols="30" rows="6">customer message here</textarea>
<input type="hidden" name="thankyou_url" value="http://www.website.co.uk/html/couple_uploaded.html"/>
<input type="submit" value="Submit Photo"/>
</form>
<?php
$to = $_POST['myEmail'];
$from = $_POST['customerEmail'];
$name = $_POST['customerName'];
$subject = $_POST['subject'];
$message = $_POST['message']."\n\n" . $name;
$message = wordwrap($message,70);
foreach ($_FILES['photo']['name'] as $key => $tmp_name) {
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["photo"]["name"][$key]);
$extension = end($temp);
if ((($_FILES["photo"]["type"][$key] == "image/gif")
|| ($_FILES["photo"]["type"][$key] == "image/jpeg")
|| ($_FILES["photo"]["type"][$key] == "image/jpg")
|| ($_FILES["photo"]["type"][$key] == "image/pjpeg")
|| ($_FILES["photo"]["type"][$key] == "image/x-png")
|| ($_FILES["photo"]["type"][$key] == "image/png"))
&& ($_FILES["photo"]["size"][$key] < 3000000)
&& in_array($extension, $allowedExts)) {
/* GET File Variables */
$tmpName = $_FILES['photo']['tmp_name'][$key];
$fileType = $_FILES['photo']['type'][$key];
$fileName = $_FILES['photo']['name'][$key];
/* Start of headers */
$headers = "From: $from";
if (file($tmpName)) {
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
} else {
echo "Invalid File.";
}
}
$sent = mail($to, $subject, $message, $headers);
if ($sent) {
echo '<meta http-equiv="refresh" content="0;url=http://website.co.uk/html/thankyou.html">';
} else {
echo "Sorry, we have encoutered a problem sending your message. Please try again or get in touch via the contact page";
}
?>
名称:
电子邮件:
照片:
第二张照片(可选):
注意:此处为客户留言
上传程序PHP:
<form enctype="multipart/form-data" method= "post" action="couple_uploader_test.php">
<input type="hidden" name="myEmail" value="info@website.co.uk">
<input type="hidden" name="subject" value="Couple Portrait Order">
<label>Name:</label><input type="text" name="customerName" required="true"/><br/>
<label>Email:</label><input type="email" name="customerEmail" required="true"/><br/>
<label>Photo:</label><input type="file" name="photo[]" accept="image/*" required="true"/>
<label>2nd Photo (optional):</label><input type="file" name="photo[]" accept="image/*"/>
<label>Notes:</label><textarea name="message" cols="30" rows="6">customer message here</textarea>
<input type="hidden" name="thankyou_url" value="http://www.website.co.uk/html/couple_uploaded.html"/>
<input type="submit" value="Submit Photo"/>
</form>
<?php
$to = $_POST['myEmail'];
$from = $_POST['customerEmail'];
$name = $_POST['customerName'];
$subject = $_POST['subject'];
$message = $_POST['message']."\n\n" . $name;
$message = wordwrap($message,70);
foreach ($_FILES['photo']['name'] as $key => $tmp_name) {
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["photo"]["name"][$key]);
$extension = end($temp);
if ((($_FILES["photo"]["type"][$key] == "image/gif")
|| ($_FILES["photo"]["type"][$key] == "image/jpeg")
|| ($_FILES["photo"]["type"][$key] == "image/jpg")
|| ($_FILES["photo"]["type"][$key] == "image/pjpeg")
|| ($_FILES["photo"]["type"][$key] == "image/x-png")
|| ($_FILES["photo"]["type"][$key] == "image/png"))
&& ($_FILES["photo"]["size"][$key] < 3000000)
&& in_array($extension, $allowedExts)) {
/* GET File Variables */
$tmpName = $_FILES['photo']['tmp_name'][$key];
$fileType = $_FILES['photo']['type'][$key];
$fileName = $_FILES['photo']['name'][$key];
/* Start of headers */
$headers = "From: $from";
if (file($tmpName)) {
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
} else {
echo "Invalid File.";
}
}
$sent = mail($to, $subject, $message, $headers);
if ($sent) {
echo '<meta http-equiv="refresh" content="0;url=http://website.co.uk/html/thankyou.html">';
} else {
echo "Sorry, we have encoutered a problem sending your message. Please try again or get in touch via the contact page";
}
?>
理想情况下,我需要它循环2种输入文件类型(第二种是可选的,因此如果用户只上传一个图像,它仍然可以工作)并将它们附加到一封电子邮件。谢谢。当循环运行时,它会从头重写$message和$headers变量,而不是串联。因此,当您实际发送电子邮件时,附加的文件只有一个。将
$headers
和$message
移动到循环的外部。您已经将$message
置于外部,但您正在用此代码覆盖它:
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
应该是:
/* Multipart Boundary above message */
$message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
PS:您可以在PHP中重定向,如下所示:
header('Location: http://website.co.uk/html/thankyou.html');
另一个提示:在尝试访问之前,您应该先检查$\u POST
数组中设置的内容,以防止出现错误:
$to = isset($_POST['myEmail']) ? $_POST['myEmail'] : NULL;
这是一个三元运算符,基本上是以下内容的简写:
if(isset($_POST['myEmail'])) {
$to = $_POST['myEmail']
} else {
$to = NULL;
}
如果您正确地使用foreach循环,一次一个地获取每个上载的文件,以便处理它,那么最终会得到类似的结果
我没有检查header的实际内容是否正确,我只是假设您做得对,但我确实简化了一些编码,并删除了不必要的标量变量创建,以将$\u FILES数组中已经存在的数据保持在完全可用的状态
/* a boundary string */
$randomVal = md5(time());
$allowedExts = array("gif", "jpeg", "jpg", "png");
/* Start of headers */
$headers = "From: $from";
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
foreach ($_FILES['photo'] as $filenumber => $file) {
$temp = explode(".", $file["name"]);
$extension = end($temp);
if ( ($file["type"] == "image/gif"
|| $file["type"] == "image/jpeg"
|| $file["type"] == "image/jpg"
|| $file["type"] == "image/pjpeg"
|| $file["type"] == "image/x-png"
|| $file["type"] == "image/png" )
&& $file["size"] < 3000000
&& in_array($extension, $allowedExts))
{
if ( file($file['tmp_name']) ) {
/* Reading file ('rb' = read binary) */
$fh = fopen($file['tmp_name'],'rb');
$data = fread($fh,filesize($file['tmp_name']));
fclose($fh);
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$file['type']};\n" .
" name=\"{$file['name']}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
} // endif
} // endforeach
/*边界字符串*/
$randomVal=md5(time());
$allowedExts=数组(“gif”、“jpeg”、“jpg”、“png”);
/*标题的开始*/
$headers=“From:$From”;
$mimeBoundary=“==Multipart_Boundary_x{$randomVal}x”;
/*文件附件的标题*/
$headers.=“\n时间版本:1.0\n”;
$headers.=“内容类型:多部分/混合;\n”;
$headers.=“boundary=\”{$mimebundary}\”;
/*消息上方的多部分边界*/
$message=“这是MIME格式的多部分消息。\n\n”。
“{$mimebundary}\n”。
“内容类型:文本/普通;字符集=\”iso-8859-1\“\n”。
“内容传输编码:7bit\n\n”。
$message。“\n\n”;
foreach($_文件['photo']为$filenumber=>$file){
$temp=explode(“.”,$file[“name”]);
$extension=end($temp);
如果($file[“type”]=“image/gif”
||$file[“type”]=“image/jpeg”
||$file[“type”]=“image/jpg”
||$file[“type”]=“image/pjpeg”
||$file[“type”]=“image/x-png”
||$file[“type”]=“image/png”)
&&$file[“size”]<3000000
&&in_数组($extension$allowedExts))
{
如果(文件($file['tmp_name']){
/*正在读取文件('rb'=读取二进制文件)*/
$fh=fopen($file['tmp_name'],'rb');
$data=fread($fh,filesize($file['tmp_name']);
fclose($fh);
/*编码文件数据*/
$data=chunk_split(base64_encode($data));
/*将附件文件添加到消息*/
$message.=“--{$mimebundary}\n”。
“内容类型:{$file['Type']};\n”。
“名称=\”{$file['name']}\“\n”。
“内容传输编码:base64\n\n”。
$data。“\n\n”。
“{$mimebundary}--\n”;
}
}//endif
}//endforeach
如果您不想费心以可读的方式格式化代码,我们为什么要费心阅读它呢?对不起,我不确定您想要的是什么。(我觉得它很可读)这是我的第一篇帖子,我被卡住了,所以我只是复制了我的代码,因为到目前为止我已经有了它,所以我们可以尝试找出它的错误-我是一个新手,它对我有用。删除元刷新,让我们回显你的文件名。到底什么不起作用?所以我想我必须这样做!完成了任何基本的调试,比如var\u dump($\u文件)
查看您真正收到了什么?您还只是假设上传永远不会失败。在$u文件中有一个['error']
参数是有原因的。另外,…文件()
将整个文件读取到一个数组中,然后将数组扔掉。这效率非常低。为什么不将读取($file)呢
还是别的什么?