PHP脚本:
<?php
require_once '../lib/PHPMailer/PHPMailerAutoload.php';
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
//$email_to = "hidden";
//$email_subject = "Request for Portfolio check up from ".$first_name." ".$last_name;
$title = array('Geslacht', 'Meneer', 'Mevrouw');
$selected_key = $_POST['title'];
$selected_val = $title[$_POST['title']];
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$email_message = "";
$email_message .="Title: ".$selected_val."\n";
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
$allowedExts = array("doc", "docx", "pdf", "PDF");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword"))
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "<script>alert('Error: " . $_FILES["file"]["error"] ."')</script>";
}
else
{
$d='C:/wamp/www//upload/';
$de=$d . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES["file"]["tmp_name"], $de)) {
echo "<script>alert('File Uploaded')</script>";
}else {
echo "<script>alert('File not Uploaded')</script>";
};
$fileName = $_FILES['file']['name'];
$filePath = $_FILES['file']['tmp_name'];
//add only if the file is an upload
}
}
else
{
echo "<script>alert('Not PDF or Word File!!')</script>";
}
echo "<script>alert('Mail being created...')</script>";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
//Create a new PHPMailer instance
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPDebug = 2;
$mail->Host = 'send.one.com';
$mail->Port = 465;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username ='';
//Password to use for SMTP authentication
$mail->Password = '';
//Set who the message is to be sent from
$mail->SetFrom($email_from, $first_name.' '.$last_name);
//Set an alternative reply-to address
//$mail->AddReplyTo('replyto@example.com','First Last');
//Set who the message is to be sent to
$mail->AddAddress(', 'Bora Urfali');
$mail->Subject = "Sollicitatie van $first_name ";
$mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);
//Send the message, check for errors
if(!$mail->Send()) {
echo "<script>alert('Something went wrong! Please try again!')</script>";
} else {
echo "<script>alert('Mail Send!')</script>";
}
}
我上传的文件正在此目录中出现:
C:\wamp\www\upload\
我仍然收到错误,无法访问此目录中的文件:
C:\wamp\tmp\file.tmp
错误是:无法访问文件:C:\ wamp \ tmp \ phpF6A9.tmp
问题:
为什么不在正确的目录中搜索文件?
答案 0 :(得分:4)
在脚本末尾附近添加$_FILES['file']['tmp_name']
作为附件
$mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);
但是文件不再存在了,因为你之前从那里移动了30行:
if (move_uploaded_file($_FILES["file"]["tmp_name"], $de)) {
你应该:
$mail->AddAttachment($de, $_FILES['file']['name']);
但在附加文件之前,您还应检查文件是否已上传并成功移动。