嗨,女士们,先生们,我希望你们能帮助我解决这个问题。
我创建了一个网络表单,人们可以在其中输入注册信息并将图像上传到我们的服务器以进行摄影比赛。上传器工作得很好,并且会发送一封包含注册信息的电子邮件,但我很难将上传的图像附加到电子邮件中。我希望有人能够帮助我解决这个问题。表单和处理器页面位于同一目录中,图像放置在名为 upload 的子文件夹中。这是我到目前为止所拥有的不适用于附加图像的内容。它给了我一个错误:
<?php
if ((($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
echo "nFile submitted.";
}
}
}
else
{
echo "Invalid file";
}
// DO THE MAILING HERE
$fields = array('name','address','city','state','zip','phone','photo','location');
//$to = "cboyd@dacc.edu";
$to2 = "ampeck@earthlink.net";
$subject = "Sustainability Photo Contest";
$headers = "MIME-Version: 1.0rn"
."Content-Type: multipart/mixed; boundary="$bound_text"";
$headers .='Content-type: image/jpeg; charset=iso-8859-1' . PHP_EOL;
$headers .= "From: noreply@dacc.edurn" . "X-Mailer: php";
$bound_text = "DACCreport";
$bound = "--".$bound_text."rn";
$bound_last = "--".$bound_text."--rn";
$message = "If you can see this MIME than your client doesn't accept MIME types!rn"
.$bound;
$message .= "Content-Type: image/jpeg; charset="iso-8859-1"rn"
."Content-Transfer-Encoding: 7bitrnrn"
."Photo Contest entryrn"
.$bound;
$file = file_get_contents($_FILES["file"]["name"]);
$message .= "Content-Type: image/jpeg; name="($_FILES["file"]["name"])"rn"
."Content-Transfer-Encoding: base64rn"
."Content-disposition: attachment; file="($_FILES["file"]["name"])"rn"
."rn"
.chunk_split(base64_encode($file))
.$bound_last;
$greet = "The following was submitted on " . date("F j, Y, g:i a") . "<p>";
$body = $greet;
//$cn = 1;
foreach($fields as $efield) {
if(isset($_POST[$efield])) {
if($efield == "name") {
$body.= "<strong>Name: </strong>" . $_POST[$efield] . "<p>";
} elseif($efield == "address") {
$body.= "<strong>Address:</strong> " . $_POST[$efield] . "<p>";
} elseif($efield == "city") {
$body.= "<strong>City:</strong> " . $_POST[$efield] . "<p>";
} elseif ($efield == "state") {
$body.= "<strong>State: </strong>" . $_POST[$efield] . "<p>";
} elseif ($efield == "phone") {
$body.= "<strong>Daytime Phone Number: </strong>" . $_POST[$efield] . "<p>";
} elseif ($efield == "photo") {
$body.= "<strong>Name of Photo: </strong>" . $_POST[$efield] . "<p>";
} elseif ($efield == "location") {
$body.= "<strong>Location where Photo was taken:</strong> " . $_POST[$efield] . "<p>";
}
}
}
//mail($to, $subject, $body, $headers);
mail($to2, $subject, $body, $headers);
?>
这是我提出的解决方案。根据上传代码和附加文件信息,需要一些微调。另外,我在标题方面遇到了问题,但我设法让它工作并且效果很好。
<?php
if ((($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
echo "<br> File submitted.";
}
}
}
else
{
echo "Invalid file";
}
// DO THE MAILING HERE
$filename = "upload/" . $_FILES["file"]["name"];
;
$to3 =
$to4 =
$subject = 'Sustainability Photo Contest';
$bound_text = "dacc1231";
$bound = "--".$bound_text."rn";
$bound_last = "--".$bound_text."--rn";
$headers = "MIME-Version: 1.0rn"
."Content-Type: multipart/mixed; boundary="$bound_text"". PHP_EOL;
$headers .= "From: noreply@dacc.edurn";
$fields = array('name','address','city','state','zip','phone','photo','location');
$greet = "The following was submitted on " . date("F j, Y, g:i a") . "<p>";
$body = $greet;
//$cn = 1;
foreach($fields as $efield) {
if(isset($_POST[$efield])) {
if($efield == "name") {
$body.= "<strong>Name: </strong>" . $_POST[$efield] . "<p>";
} elseif($efield == "address") {
$body.= "<strong>Address:</strong> " . $_POST[$efield] . "<p>";
} elseif($efield == "city") {
$body.= "<strong>City:</strong> " . $_POST[$efield] . "<p>";
} elseif ($efield == "state") {
$body.= "<strong>State: </strong>" . $_POST[$efield] . "<p>";
} elseif ($efield == "phone") {
$body.= "<strong>Daytime Phone Number: </strong>" . $_POST[$efield] . "<p>";
} elseif ($efield == "photo") {
$body.= "<strong>Name of Photo: </strong>" . $_POST[$efield] . "<p>";
} elseif ($efield == "location") {
$body.= "<strong>Location where Photo was taken:</strong> " . $_POST[$efield] . "<p>";
}
}
}
$message = "If you can see this MIME than your client doesn't accept MIME types!rn"
.$bound;
$message .= "Content-Type: text/html; charset="iso-8859-1"rn"
."Content-Transfer-Encoding: 7bitrnrn"
."Here is a Submission for the Photo Contestrn" .$body ."rn"
.$bound;
$file = file_get_contents($filename);
$message .= "Content-Type: image/jpg; name="".$filename.""rn"
."Content-Transfer-Encoding: base64rn"
."Content-disposition: attachment; file="".$filename.""rn"
."rn"
.chunk_split(base64_encode($file))
.$bound_last;
if(mail($to3, $subject, $message, $headers)
&&
mail($to4, $subject, $message,$headers ))
{
echo '<br> MAIL SENT';
} else {
echo '<br> MAIL FAILED';
}
?>
$files = $_FILES['filesToUpload']['tmp_name'];
$filename = $_FILES['filesToUpload']['name'];
$fp = fopen($files[$i],"rb");
$data = fread($fp,filesize($files[$i]));
fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name="".basename($filename[$i]).""n" .
"Content-Description: ".basename($filename[$i])."n" .
"Content-Disposition: attachment;n" . " filename="".basename($filename[$i]).""; size=".filesize($files[$i]).";n" .
"Content-Transfer-Encoding: base64nn" . $data . "nn";
您可以使用此代码附加文件,而无需将其移动到服务器。