我在提供文件上传路径时被困,我有一个html文件输入名称uploadfile,我使用phpmailer()发送附件。请帮帮我,我需要php开发人员的支持。这是我的代码告诉我为什么我得到无法访问文件错误。我的电子邮件没有附件,只有消息即将到来。
<div class="field">
<label for="Browse">File to upload: <span class="required">*</span></label>
<div class="inputs">
<input name="uploadedfile" id="uploadedfile" type="file" style=" height: 37px;" name="MAX_FILE_SIZE" value="100000" />
</div>
</div>
//Form Fields
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$verify = isset($_POST["verify"]) ? $_POST["verify"] : "";
$path = $_FILES["uploadedfile"]["name"];
echo $path;
$encoding = 'base64';
$type = 'application/octet-stream';
$objmail->From = $email;
$objmail->FromName = $name;
$objmail->AddAddress($toAddress, $toName);
$objmail->AddReplyTo($email, $name);
$objmail->Subject = $email_subject;
$objmail->MsgHTML($email_body);
$objmail->AddAttachment($path,$encoding,$type);
if(!$objmail->Send()) {
$error = "Message sending error: ".$objmail->ErrorInfo;
}
}
答案 0 :(得分:3)
两个问题:
首先:
$path = $_FILES["uploadedfile"]["name"];
$objmail->AddAttachment($path,$encoding,$type);
$path
将成为CLIENT计算机上文件的名称。这与PHP存储的临时文件完全无关,该文件列在['tmp_name']
中。
第二:
您根本不会验证是否发生了上传,而只是假设它已成功。至少,您需要
if ($_FILES['uploadedfile']['error'] === UPLOAD_ERR_OK) {
$objmail->AddAttachment(
$_FILES['uploadedfile']['tmp_name'], // temp location on server
$_FILES['uploadedfile']['name'], // name that appears in email
'base64',
$_FILES['uploadedfile']['type'] // mime type provided by uploader
);
}