为什么我的PHPmailer脚本不能用于获取/保存文件?

时间:2011-07-14 15:27:29

标签: php file file-upload phpmailer

我有一个用循环编写的HTML / javascript表单,用于上传无限量的文件,其中包含names =“file1”,“file2”等。 (I ++)

所以现在我有一个PHP表单来处理它(获取所有文件,保存到临时文件夹“上传”,并使用phpmailer作为附件发送电子邮件)。

<?php
require("class.phpmailer.php");
//Variables Declaration
$name = "the Submitter";
$email_subject = "Images Attachment";
$Email_msg ="A visitor submitted the following :\n";
$Email_to = "you@yourSite.com"; // the one that recieves the email
$email_from = "someone@someone.net";
$dir = "uploads/$filename";
chmod("uploads",0777);
$attachments = array();

uploadFile();
//

//
//==============upload File Function============\\
//
function uploadFile() {
global $attachments;
while(list($key,$value) = each($_FILES[images][name]))
{
//
if(!empty($value))
{
$filename = $value;
//the Array will be used later to attach the files and then remove them from server ! array_push($attachments, $filename);
$dir = "uploads/$filename";
chmod("uploads",0777);
$success = copy($_FILES[images][tmp_name][$key], $dir);
}
//
}
//
if ($success) {
echo " Files Uploaded Successfully<BR>";
SendIt();
//
}else {
exit("Sorry the server was unable to upload the files...");
}
//
}
//
//==== PHP Mailer With Attachment Func ====\\
//
function SendIt() {
//
global $attachments,$name,$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->FromName = $name;
$mail->AddAddress($Email_to);
$mail->AddReplyTo($email_from);
$mail->WordWrap = 50;// set word wrap
//now Attach all files submitted
foreach($attachments as $key => $value) { //loop the Attachments to be added ...
$mail->AddAttachment("uploads"."/".$value);
}
$mail->Body = $Email_msg."Name : ".$name."\n";
//
$mail->IsHTML(false);// send as HTML
$mail->Subject = $email_subject;
if(!$mail->Send())
{
echo "Message was not sent <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
//
echo "Message has been sent";
// after mail is sent with attachments , delete the images on server ...
foreach($attachments as $key => $value) {//remove the uploaded files ..
unlink("uploads"."/".$value);
}
//
}
//
?>

但由于某种原因,我得到以下错误:

注意:未定义的变量:第10行/usr/home/jak2234/public_html/new_form/phpmailerprocess.php中的文件名

Warning: chmod() [function.chmod]: Operation not permitted in /usr/home/jak2234/public_html/new_form/phpmailerprocess.php on line 11

Notice: Use of undefined constant images - assumed 'images' in /usr/home/jak2234/public_html/new_form/phpmailerprocess.php on line 22

Notice: Use of undefined constant name - assumed 'name' in /usr/home/jak2234/public_html/new_form/phpmailerprocess.php on line 22

Warning: Variable passed to each() is not an array or object in /usr/home/jak2234/public_html/new_form/phpmailerprocess.php on line 22

Notice: Undefined variable: success in /usr/home/jak2234/public_html/new_form/phpmailerprocess.php on line 36
Sorry the server was unable to upload the files...

如果有人可以帮助解决这些问题,或者提供更好的方法,那将会非常有用。

非常感谢你!

1 个答案:

答案 0 :(得分:1)

第一个警告:您正在对uploads目录进行chmodding。除非该目录由您的网络服务器的用户ID拥有,否则您将获得权限被拒绝错误 - 您无法修改不属于您的内容。

第二次和第三次警告:$_FILES数组中的数组键不正确。他们应该是

while(list($key,$value) = each($_FILES['images']['name']))

注意引号 - 没有引号,PHP假定它们是通过define()创建的常量。如果该名称没有常量,PHP礼貌会将它们视为同名的字符串,但会发出警告。

第四次警告:你误导each()。相反,只需要:

foreach($_FILES['images']['name'] as $key => $value) {

第五个警告:当文件复制成功时,您为$success分配值的唯一位置是在进行映像复制的if()范围内。由于代码被破坏,副本永远不会发生,因此永远不会定义$success。要修复,请输入

$success = false;

位于文件顶部的某处,因此使用默认值定义。

除此之外,请勿对上传的文件使用copy()。 PHP和共享服务器上的文件上传涉及安全问题。请改用move_uploaded_file()。这也将是一个便宜得多的操作,因为在单个文件系统中移动文件几乎是即时的,而copy()实际上复制了文件 - 在大文件上这很快就会变得昂贵(时间+ cpu +磁盘空间)。


评论后续:

如何命名输入字段并不重要,因为这将决定文件数组中的内容。

如果您选择file1file2等选项,最终会得到:

$_FILES = array(
    'file1' => array('name' => ..., 'size' => ..., etc...),
    'file2' => array('name' => ..., 'size' => ..., etc...),
    etc...
)

如果您执行files[],最终会得到:

$_FILES = array (
   'files' => array
        'name' => array(
            0 = 'name of first file',
            1 = 'name of second file',
            ...
        'size' => array(
            0 = 'size of first file'
            1 = 'name of second file'
         etc...

主要的结构差异。我不知道为什么允许这样一种愚蠢的差异投入生产,但由于这是PHP的核心功能,因此将其更改为更符合逻辑的基本上是不可能的。