我正在尝试将图像作为附件发送到表单上。
但在点击提交后,我看到"Sorry the server was unable to upload the files...".
如果有人知道原因,请告诉我。 提前谢谢。
<?php
require("class.phpmailer.php");
$name = "the Submitter";
$email_subject = "photo Attachment";
$Email_msg ="A visitor submitted the following :\n";
$Email_to = "someone1@someone.com";
$email_from = "someone2@someone.net";
$dir = "uploads/$filename";
chmod("uploads",0777);
$attachments = array();
checkType();
function checkType() {
while(list($key,$value) = $_FILES[photo][type]){
strtolower($value);
if($value != "image/jpeg" AND $value != "image/pjpeg" AND $value != "") {
exit('Sorry , current format is <b>'.($value).'</b> ,only Jpeg or jpg are allowed.') ;
}
}
checkSize();
}
function checkSize(){
while(list($key,$value) = ($_FILES[photo][size]))
{
$maxSize = 5000000;
if(!empty($value)){
if ($value > $maxSize) {
echo"Sorry this is a very big file .. max file size is $maxSize Bytes = 5 MB";
exit();
}
else {
$result = "File size is ok !<br>";
}
}
}
uploadFile();
}
function uploadFile() {
global $attachments;
while(list($key,$value) = ($_FILES[photo][name]))
{
if(!empty($value))
{
$filename = $value;
$dir = "uploads/$filename";
chmod("uploads",0777);
$success = copy($_FILES[photo][tmp_name][$key], $dir);
}
}
if ($success) {
echo " Files Uploaded Successfully<BR>";
SendIt();
}else {
exit("Sorry the server was unable to upload the files...");
}
}
function SendIt() {
global $attachments,$name,$Email_to,$Email_msg,$email_subject,$email_from;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = false;
$mail->From = $email_from;
$mail->FromName = $name;
$mail->AddAddress($Email_to);
$mail->AddReplyTo($email_from);
$mail->WordWrap = 50;
foreach($attachments as $key => $value) {
$mail->AddAttachment("uploads"."/".$value);
}
$mail->Body = $Email_msg."Name : ".$name."\n";
$mail->IsHTML(false);
$mail->Subject = $email_subject;
if(!$mail->Send())
{
echo "Message was not sent <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
foreach($attachments as $key => $value) {
unlink("uploads"."/".$value);
}
}
?>
答案 0 :(得分:0)
感谢您回答我的问题。最后,它奏效了。 以下是有效的。
<?php
require("PATH/PHPMailer-master/class.phpmailer.php");
$name = "the Submitter";
$email_subject = "photo Attachment";
$Email_msg ="A visitor submitted the following :\n";
$Email_to = "someone1@someone.com";
$email_from = "someone2@someone.net";
$dir = "uploads/$filename";
chmod("uploads",0777);
chmod("PATH/uploads",0777);
$attachments = array();
checkType();
function checkType() {
strtolower($_FILES[photo][type]);
if($value != "image/jpeg" AND $value != "image/pjpeg" AND $value != "") {
exit('Sorry , current format is <b>'.($value).'</b> ,only Jpeg or jpg are allowed.') ;
}
checkSize();
}
function checkSize(){
$value = $_FILES[photo][size];
$maxSize = 5000000;
if(!empty($value)){
if ($value > $maxSize) {
echo"Sorry this is a very big file .. max file size is $maxSize Bytes = 5 MB";
exit();
}
else {
$result = "File size is ok !<br>";
}
}
uploadFile();
}
function uploadFile() {
global $attachments;
$value = $_FILES[photo][name];
if(!empty($value))
{
$filename = $value;
$dir = "PATH/uploads/$filename";
$success = move_uploaded_file($_FILES[photo][tmp_name], $dir);
if($success){
$attachments = $value;
}
}
if ($success) {
echo " Files Uploaded Successfully<BR>";
SendIt();
}else {
exit("Sorry the server was unable to upload the files...");
}
}
function SendIt() {
global $attachments,$name,$Email_to,$Email_msg,$email_subject,$email_from;
$mail = new PHPMailer();
$mail->IsSMTP();//
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->SMTPDebug = 2;
$mail->Port = 25;
$mail->Username = "xxx@gmail.com";
$mail->Password = "xxx";
$mail->From = $email_from;
$mail->FromName = $name;
$mail->AddAddress($Email_to);
$mail->AddReplyTo($email_from);
$mail->WordWrap = 50;
foreach($attachments as $key => $value) {
$mail->AddAttachment("PATH/uploads"."/".$value);
}
$mail->Body = $Email_msg."Name : ".$name."\n";
$mail->IsHTML(false);
$mail->Subject = $email_subject;
if(!$mail->Send())
{
echo "Message was not sent <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
foreach($attachments as $key => $value) {
unlink("PATH/uploads"."/".$value);
}
}
?>