为什么我的PHPmailer脚本无法工作来获取/保存文件? [英] Why wont my PHPmailer script work to get / save files?

查看:66
本文介绍了为什么我的PHPmailer脚本无法工作来获取/保存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用循环编写的HTML/javascript表单,可以上传数量不限的文件,名称为"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...

如果有人可以提供任何帮助或提供更好的输入方法,那将非常有帮助.

非常感谢你!

解决方案

第一个警告:您正在阻塞uploads目录本身.除非该目录归您的Web服务器用户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的核心功能,因此将其更改为在逻辑上更加一致的做法基本上是不可能的.

I have an HTML / javascript form written with a loop to upload an unlimited amount of files with names="file1","file2",etc. (i++)

So now i have a PHP form to process it (get all files, save to temporary folder "uploads", and email as attachments using 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);
}
//
}
//
?>

But for some reason, im getting the following errors:

Notice: Undefined variable: filename in /usr/home/jak2234/public_html/new_form/phpmailerprocess.php on line 10

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...

If someone can help with ANY of these or give their input of a better way to do it, that would be so so helpful.

Thank you soooo much!!!

解决方案

First warning: You're chmodding the uploads directory itself. Unless the directory is owned by your webserver's user ID, you'll get the permission denied error - you cannot chmod something that does not belong to you.

Second and Third warning: Your array keys in the $_FILES array are incorrect. They should be

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

note the quotes - without the quotes, PHP assumes they're constants that were created via define(). If there's no constant by that name, PHP polite will treat them as strings of the same name, but issues the warning.

Fourth warning: You're misusing each(). Instead, just have:

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

Fifth warning: The only place you assign a value to $success is within the if() that does the image copy, when the file copy succeeds. Since your code is broken, the copy never takes place, so $success is never defined. To fix, put a

$success = false;

somewhere at the top of your file, so it's defined with a default value.

Beyond that, don't use copy() on uploaded files. There's security issues involved with PHP and file uploads on shared servers. Use move_uploaded_file() instead. This will be a far cheaper operation as well, since moving a file within a single filesystem is near instantaneous, while copy() actually duplicates the file - on large files this gets expensive (time + cpu + disk space) very quickly.


comment followup:

It does matter how you name the input fields, as that'll determine how things show up in the files array.

If you go with the file1, file2, etc... option, you end up with:

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

If you do files[], you end up with:

$_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...

A major structural difference. I have no idea why such a moronic difference was allowed to go into production, but since this is core PHP functionality, changing it to be more logically consistent is basically impossible.

这篇关于为什么我的PHPmailer脚本无法工作来获取/保存文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆