Untitled UI logotext
Solutions
WebsitesEcommerceMobile AppsWeb AppsProduction Support & Maintenance
Our work
Company
About usBlogPodcastContact us
Book a free consultation

PHPMailer Attachment Error

Olivia Rhye

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.

Ready to start a project?

Book a free consultation
Untitled UI logotext
Our work
About us
Blog
Careers
Submit a ticket
Agency Partnerships
Contact
© 2024 fjorge. All rights reserved.
Privacy