<?php
$msg = '';
if (array_key_exists('userfile', $_FILES))
//$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name']));
$fileName = $_FILES["userfile"]["name"];
$fileTmpLoc = $_FILES["userfile"]["tmp_name"];
$pathAndName = "uploads/".$fileName;
$moveResult = move_uploaded_file($fileTmpLoc, $pathAndName);
if ($moveResult == true) {
// Upload handled successfully
// Now create a message
// This should be somewhere in your include_path
require_once '/home/jaydeepkanada/public_html/phpMailer/PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->setFrom('accounts@newedgesecurity.in', 'Mail From Website');
$mail->addAddress('jaydeepkanada@gmail.com', 'Jaydeep Kanada');
$mail->Subject = 'PHPMailer file sender';
$mail->msgHTML("My message body");
$allowedExts = array("doc", "docx", "pdf");
$extension = end(explode(".", $_FILES["userfile"]["name"]));
if (in_array($extension, $allowedExts))
{
$mail->addAttachment($uploadfile, $_FILES["userfile"]["name"]);
if (!$mail->send()) {
$msg = "Mailer Error: " . $mail->ErrorInfo;
} else {
$msg = "Message sent!";
}
}else{
die("File type not supported. Only PDF or DOC.");
}
} else {
$msg = 'Failed to move file to ' . $uploadfile;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>PHPMailer Upload</title>
</head>
<body>
<?php if (empty($msg)) { ?>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="100000"> Send this file: <input name="userfile" type="file">
<input type="submit" value="Send File">
</form>
<?php } else {
echo $msg;
} ?>
</body>
</html>
我正在使用PHP mailer发送带有附件的邮件,在此代码路径是正确的,但它总是显示未能移动路径。
使用正确的文件大小转换MAX_FILE_SIZE值,例如10485760
问题是<input type="hidden" name="MAX_FILE_SIZE" value="100000">
标签内的值以字节为单位,你上传的文件可能大于97kbytes(100000字节=> 97.66 kbytes),所以导致系统给出一个错误(如果你看$_FILES["userfile"]['error']或$_FILES["userfile"]['error'][0]内容,你会看到这是给你错误2,这意味着"上传的文件超过了MAX_FILE_SIZE指令是在HTML表单中指定的"。