所以我搜索了很多类似的问题,但似乎无法找到解决方案。
我有联系表单,我希望它能够发送多个图像。我现在的代码有三个输入字段用于图片,但现在我只想出了如何获取其中一个图片上传和发送。我很确定我需要某种循环或数组,但这不是我的强项。
HTML
<form action="test.php" method="post" autocomplete="on" enctype="multipart/form-data" accept-charset='UTF-8'>
<fieldset><label for="Name">Name <sup>*</sup></label>
<input name="name" placeholder="full name" id="Name" required="" autofocus><br>
<label for="email">E-mail <sup>*</sup></label>
<input type="email" name="email" placeholder="youremail@domain.com" id="email" required=""><br>
<label for="phoneNumber">Phone # <sup>*</sup></label>
<input type="tel" name="phone" placeholder="XXX-XXX-XXXX" id="phoneNumber" required=""><br>
</fieldset>
<fieldset>
<label for="year">Year <sup>*</sup></label>
<input type="text" name="year" placeholder="year of car" id="year" required=""><br>
<label for="make">Make <sup>*</sup></label>
<input type="text" name="make" placeholder="make of car" id="make" required=""><br>
<label for="model">Model <sup>*</sup></label>
<input type="text" name="model" placeholder="model of car" id="model" required=""><br>
</fieldset>
<label for="State" class="state">State <sup>*</sup></label><br>
<input type="text" class="state" name="state" id="state" placeholder="Do not enter if you are human">
<fieldset>
<legend>Add Photos <sup>*</sup></legend>
<input type="file" name="attachment" required=""><br>
<input type="file" name="attachment"><br>
<input type="file" name="attachment"><br>
</fieldset>
<label for="comments">Comments</label><br>
<textarea style="float: left;" rows="4" name="comment" id="comments" placeholder="additional comments"></textarea>
<br>
<input class="button" type="submit" name="submit" value="Send" class="button">
<p class="required"><sup>*</sup> denotes a required field.</p>
</form>
PHP
<?php
$to = '';
$name = $_POST['name'];
$email = $_POST ['email'];
$phone = $_POST['phone'];
$year = $_POST['year'];
$make = $_POST['make'];
$model = $_POST['model'];
$comment = $_POST['comment'];
$state = $_POST['state'];
$message = "
Name: $name
E-mail: $email
Phone: $phone
Year: $year
Make: $make
Model: $model
Message: $comment
";
if($_POST['state'] != ''){
echo "It appears you are a bot!";
exit();
}
else{
//process the rest of the form
}
/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
/* Start of headers */
$headers = "From: $name $email";
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";
}
$flgchk = mail ("$to", "$subject", "$message", "$headers");
if($flgchk){
echo "A email has been sent. We will get back to you as soon as
possible.";
}
else{
echo "Error in Email sending";
}
?>
答案 0 :(得分:1)
您的所有三个字段都被命名为&#34;附件&#34;。给他们一个独特的名字,如&#34; attachment1&#34;,&#34; attachment2&#34;等。
然后在上传时使用此功能:
for ($x = 1; $x <= $numberOfFiles; ++$x) {
$tmpName = $_FILES['attachment'.$x]['tmp_name'];
$fileType = $_FILES['attachment'.$x]['type'];
$fileName = $_FILES['attachment'.$x]['name'];
// do your upload here
}
你甚至可以在这样的循环中完成它,只要你保持命名模式一致,就可以在不改变后端代码的情况下添加更多文件字段(可能使用JavaScript)。
$x = 1;
while (isset($_FILES['attachment'.$x])) {
$tmpName = $_FILES['attachment'.$x]['tmp_name'];
$fileType = $_FILES['attachment'.$x]['type'];
$fileName = $_FILES['attachment'.$x]['name'];
// do your upload here
++$x;
}
这只会在每个周期增加x
,并检查是否使用该名称发送了一个文件。
在某人用收件箱填充大量文件和/或网络恶意之前,您可能还应该检查一些文件的大小和类型。
更新:这应该是一个基本上完整的解决方案。我目前无法为您测试它,所以它可能无法直接开箱即用,但它至少应该指向正确的方向。在此代码段中,它首先设置电子邮件的初始部分,然后循环并一次添加一个文件。
<?php
$to = '';
$name = $_POST['name'];
$email = $_POST ['email'];
$phone = $_POST['phone'];
$year = $_POST['year'];
$make = $_POST['make'];
$model = $_POST['model'];
$comment = $_POST['comment'];
$state = $_POST['state'];
$message = "Name: $name
E-mail: $email
Phone: $phone
Year: $year
Make: $make
Model: $model
Message: $comment";
if($_POST['state'] != ''){
echo "It appears you are a bot!";
exit();
}
/* Start of headers */
$headers = "From: $name $email";
/* 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";
/* Loop through files */
$x = 1;
while (isset($_FILES['attachment'.$x])) {
/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
/* Skip invalid files */
if (!file($tmpName)) {
++$x;
continue;
}
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* 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";
++$x;
}
/* Close the message */
$message .= "--{$mimeBoundary}--\n";
$flgchk = mail ("$to", "$subject", "$message", "$headers");
if($flgchk){
echo "A email has been sent. We will get back to you as soon as possible.";
} else {
echo "Error in Email sending";
}
?>