Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBen Attenborough
Front End Web Development Techdegree Graduate 32,769 PointsAdding an attachment
Hi, I've been following the PHP Development course and the stuff about using a HTML form to send an email message has been really useful. However, I am currently working on a project where the client would like users to be able to upload a photo via the form. I am unsure how I should go about doing this. Would I need to upload the picture to the server first before attaching it to the form? Any guidance would be much appreciated!
Thanks,
Ben
2 Answers
Enara L. Otaegi
13,107 PointsYou can find some examples here about how to upload files from a form to the server. If you don't need to resize the photo or check if it's a photo or anything like that you can do something like this.
The form:
<input type="file" name="photo">
The php file:
<?php
$uploads_dir = '/uploads';
if ($_FILES["photo"]["error"] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["photo"]["tmp_name"];
$name = $_FILES["photo"]["name"];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
?>
Andrew McCormick
17,730 PointsYou mentioned attaching the file to the form which would go along with what Enara mentioned. However if you want to attach the file to an email, for example the user uploads and file through the form and then that file is sent to the recipient in the email, then I would do what Enara mentioned to upload the file and then use a library like phpmailer ( see tutorial here: http://phpmailer.worxware.com/?pg=tutorial#3 ) to attach the file from that location. hope that helps.