Why wont my PHPmailer script work to get / save files?
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 li开发者_JAVA技巧ne 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.
精彩评论