我正在使用此代码通过表单上传和附加图像,并将其发送到邮件(如html而不是smtp)。 一旦我在网页上输入这个(代码)页面,我就可以看到输出就好了,我可以点击“选择文件”并从某个目录中选择一个文件。 但是,我可以立即看到当回到页面时回显“无效文件”,可能是因为我还没有上传图像,但代码运行而不是等我选择的东西。 我错过了一些会提交我的选择而不是发送的triger吗?
当配置它像我一样发送为html时,$ mail-> Send()是否足够,当代码到达该命令时,带有附件的邮件将被发送?或者我还需要一些其他的部落来发送它?
谢谢,
<?php
include_once("functions.php");
// Process
$action = isset($_POST["action"]) ? $_POST["action"] : "";
if (empty($action))
{
$output = "<form action='#'>
<h1>header: </h1>
<label for='image'>upload: </label>
<input type='file' id='image' name='image' maxlength=50 >";
}
echo $output;
$image = $_POST["image"];
uploadImage($image);
require("class.phpmailer.php");
$Email_to = "some@gmail.com"; // the one that recieves the email
$email_from = "someone@someone.net";
$dir = "uploads/$filename";
chmod("uploads",0777);
function uploadImage($image){
if ((($_FILES["image"]["type"] == "image/gif")
|| ($_FILES["image"]["type"] == "image/jpeg")
|| ($_FILES["image"]["type"] == "image/pjpeg")
|| ($_FILES["image"]["type"] == "image/jpg")
|| ($_FILES["image"]["type"] == "image/png"))
&& ($_FILES["image"]["size"] < 2097152)
&& (strlen($_FILES["image"]["name"]) < 51)){
if ($_FILES["image"]["error"] > 0){
echo "Return Code: " . $_FILES["image"]["error"];
}
else{
echo "Upload: " . $_FILES["image"]["name"] . "<br />";
echo "Type: " . $_FILES["image"]["type"] . "<br />";
echo "Size: " . ($_FILES["image"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["image"]["tmp_name"] . "<br />";
if (file_exists("images/" . $_FILES["image"]["name"])){
echo $_FILES["image"]["name"] . " already exists. ";
}
else{
move_uploaded_file($_FILES["image"]["tmp_name"],
"images/" . $_FILES["image"]["name"]);
}
}
}else{
echo "Invalid file";
}
$filename = $_FILES["image"]["type"];
$dir = "uploads/$filename";
chmod("uploads",0777);
$success = copy($_FILES[images][tmp_name], $dir);
if ($success) {
echo " Files Uploaded Successfully<BR>";
SendIt();
}
}
function SendIt() {
//
global $attachments,$Email_to,$Email_msg,$email_subject,$email_from;
//
$mail = new PHPMailer();
//$mail->IsSMTP();// send via SMTP
//$mail->Host = "localhost"; // SMTP servers
//$mail->SMTPAuth = false; // turn on/off SMTP authentication
$mail->From = $email_from;
$mail->AddAddress($Email_to);
$mail->AddReplyTo($email_from);
$mail->WordWrap = 50;// set word wrap
//now Attach all files submitted
$mail->AddAttachment("uploads"."/".$_FILES["image"]["type"]);
//
$mail->IsHTML(true);// send as HTML
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
}
?>
答案 0 :(得分:0)
只有在有$ action时才应该触发uploadImage函数:
...previous code...
if (empty($action))
{
?>
<form action=''>
<h1>header: </h1>
<label for='image'>upload: </label>
<input type='file' id='image' name='image' maxlength=50 >
</form>
<?php
exit; // stop the upload script running
}
$image = $_POST["image"];
uploadImage($image);
require("class.phpmailer.php");
... rest of the code ...