我有一个表单,希望用户能够选择要上载的图像。图像临时上传到wp content/uploads文件夹,附加到电子邮件,然后销毁。
现在,表单正在提交,我可以看到图像正在响应标题中处理,但电子邮件中没有图像,上载文件夹中也没有图像文件。是否有人发现此代码有任何问题?
以下是表格:
<form id="shareForm" action="<?php echo get_template_directory_uri(); ?>/library/file-form.php" method="post" enctype="multipart/form-data">
<input type="text" id="name" name="name" placeholder="Name" />
<textarea rows="4" cols="50" name="message" id="message" placeholder="Message"></textarea>
<input type="file" id="photo" name="photo" class="upload">
<input type="submit" name="submit" value="Submit">
</form>
下面是我的PHP邮件脚本:<?php
require( \'../../../../wp-load.php\' );
if(isset($_POST["name"])){
$name = $_POST["name"];
$message = $_POST["message"];
$fName = str_replace(array(\' \', \',\', \'\\\'\' ), \'-\');
$date = new DateTime(null, new DateTimeZone(\'America/Chicago\'));
$myDate = $date->format("m/d/Y H:i");
$fileDate = $date->format("m-d-Y");
$homeUrl = home_url(\'/\');
if($_FILES["file"]["type"] != ""){
$allowedExts = array("jpg", "png", "gif");
$extension = end(explode(".", $_FILES["file"]["name"]));
$newFileName = $fName . $fileDate."." .$extension;
if ($_FILES["file"]["error"] > 0) {
$myPath = $homeUrl . \'share/?upload=error\';
wp_redirect($myPath); exit;
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], WP_CONTENT_DIR . "/uploads/user-submitted/" . $newFileName);
}
}
$to = \'my@email.com\';
$subject = $fName . " submitted a photo";
$message = "Date: " . $myDate . " EST \\n";
$message .= "Message: " .$message. "\\n";
$attachments = array(WP_CONTENT_DIR . "/uploads/user-submitted/".$newFileName);
$headers[] = \'From: Photo Submissions <my@email.com>\';
wp_mail( $to, $subject, $message, $headers, $attachments);
unlink(WP_CONTENT_DIR . "/uploads/user-submitted/" . $newFileName);
$myPath = $homeUrl . \'share/?upload=success\';
wp_redirect($myPath);
exit;
}
?>