PHPMailer Attachment Error

August 19, 2009

Recently I was getting an error on one of our sites. It was a simple form that has an upload field that uploads a file and emails it.

I was getting this error however:

Could Not Access: /var/…… (my path to the upload)

I immediately figured it was a permission problem of the upload file. That was all fine though. After a lot of code staring and some reading up on the $_FILES array I noticed where the issue was in my phpmailer code.

if(isset($_FILES['filename'])){
     $mail->addattachment($target_path);
}

The thing I learned is that even when a file is not picked to upload, the $_FILES array is still set albeit empty.

Once I changed my code to:

if(isset($_FILES['filename']) && $_FILES['filename'] != ''){
     $mail->addattachment($target_path);
}

All is good now! Hope this helps others save time!

That’s all good and dandy unless a file was not selected to upload.

Author

Stay in Touch!

Stay in Touch!
Solutions Architecture

browse through our blog articles

Blog Archive