wiggst3r Posted April 23, 2009 Share Posted April 23, 2009 Hi I have a form, which when users fill out the results are sent to me in an email. I'm wanting to add a filed whereby someone can add a file when they fill out the form and this file will be sent along with the email as an attachment. Does anyone know how I can do this? Any examples of code that people have used before? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/155324-upload-file-and-email/ Share on other sites More sharing options...
adam291086 Posted April 23, 2009 Share Posted April 23, 2009 google it heres one example http://www.tizag.com/phpT/fileupload.php Quote Link to comment https://forums.phpfreaks.com/topic/155324-upload-file-and-email/#findComment-817170 Share on other sites More sharing options...
wiggst3r Posted April 23, 2009 Author Share Posted April 23, 2009 Is there an easy way to prevent certain files form being uploaded? I'd only like users to be able to upload jpeg/png/gif, .doc, docx and pdf's Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/155324-upload-file-and-email/#findComment-817173 Share on other sites More sharing options...
adam291086 Posted April 23, 2009 Share Posted April 23, 2009 check the file type once uploaded with an if statement or switch statement $_FILES['attachement']['type'] Quote Link to comment https://forums.phpfreaks.com/topic/155324-upload-file-and-email/#findComment-817174 Share on other sites More sharing options...
wiggst3r Posted April 23, 2009 Author Share Posted April 23, 2009 Hi Thanks for the replies. How could I check (uing the if/switch) in my code: <?php function is_valid_email($email) { $result = TRUE; if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) { $result = FALSE; } return $result; } if(isset($_POST['submit'])) { $error = ''; $name = mysql_real_escape_string($_POST['name']); $email = mysql_real_escape_string($_POST['email']); $attached = $_POST['attached']; if(trim($name) == '') { echo '<p>Please enter your name</p>'; } if(trim($email) == '' || !is_valid_email($email)) { echo '<p>Please enter a valid email address</p>'; } else { $target_path = "temp/"; $target_path = $target_path . basename( $_FILES['attached']['name']); $target_path = $target_path . basename( $_FILES['attached']['type']); $target_path = "temp/"; $target_path = $target_path . basename( $_FILES['attached']['name']); if(move_uploaded_file($_FILES['attached']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['attached']['name']). " has been uploaded"; } else { echo "There was an error uploading the file, please try again!"; } $mime_type = "application/msword"; //Begin the message. Be sure to change this how you want it. $message = "Please find my CV attached"; $headers = "From: $email"; $to = "example@gmail.com"; $subject = "Job Application"; $from = $email; mail($to,$subject,$message,$headers); echo "<p>Thank you for submitting your details.</p>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155324-upload-file-and-email/#findComment-817196 Share on other sites More sharing options...
Yesideez Posted April 23, 2009 Share Posted April 23, 2009 check the file type once uploaded with an if statement or switch statement $_FILES['attachement']['type'] I wouldn't rely on this too much as this is set by the browser and can be faked. Quote Link to comment https://forums.phpfreaks.com/topic/155324-upload-file-and-email/#findComment-817198 Share on other sites More sharing options...
wiggst3r Posted April 23, 2009 Author Share Posted April 23, 2009 All I'd like to do is make sure that the file uploaded is either a word doc, jpg, gif, png or pdf. It can simply be a PDF or .doc file really, not fussed about the image files. I'm still not sure how I'd send the file as an attachment as I'm aware of mime-type issues and setting the right mime-type depending on the file uploaded. All help will be welcomed. Quote Link to comment https://forums.phpfreaks.com/topic/155324-upload-file-and-email/#findComment-817236 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.