fat creative Posted June 21, 2008 Share Posted June 21, 2008 I am new to php and trying to update a script I found somewhere. I want a user to be able to add their name and email address and have the option to either add comments or upload an image. The script was originally just uploading an image. I added in the comment field and it does email the comments to me. The problem is if the user just adds comments and no image. Part of the script checks for file size equals 0 (meaning they did not upload anything). I commented that out so it's not a requirement to upload an image. However, then the script checks for valid file types (images only) and still produces an error message. I can't seem to figure out how to get it to skip this section. IF the user uploads an image, then I do want it to check the size and file type. But if they don't upload an image, I just want it to skip that. The URL is here: http://www.amtrakfamilyreunions.com/email-image-upload.html My code is pasted below. If anyone could offer some suggestions, I would very much appreciate it. <?php # ---------------------------------------------------- # ----- EMAIL IMAGE UPLOAD # ----- Version 4.1 # ----- Created on: 02/04/07 # ----- Designed by: American Financing # ----- http://www.americanfinancing.net # ----- Free Appraisal! 2 Hour Pre-Approval 1 Week Closing. Now That's Service! # ----- PLEASE FEEL FREE TO MODIFY THIS SCRIPT TO YOUR NEEDS # ----- ENJOY!!! # ---------------------------------------------------- include("header.html"); @$Name = addslashes($_POST['Name']); @$email = addslashes($_POST['email']); @$story = addslashes($_POST['story']); @$upload_Name = $_FILES['upload']['name']; @$upload_Size = $_FILES['upload']['size']; @$upload_Temp = $_FILES['upload']['tmp_name']; @$upload_Mime_Type = $_FILES['upload']['type']; function RecursiveMkdir($path) { if (!file_exists($path)) { RecursiveMkdir(dirname($path)); mkdir($path, 0777); } } if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email)) { die("<p align='center'><b><font face='Arial Black' size='5' color='#FF0000'>Please enter a valid email</font></b></p>"); } /* if( $upload_Size == 0) { die("<p align='center'><b><font face='Arial Black' size='2' color='#FF0000'>ERROR<br>upload size == 0 <br>PLEASE CHECK THE FILE SIZE AND FORMAT</font></b></p>"); } */ // CHANGE THIS TO A HIGHER OR LOWER VALUE - REMEMBER HOSTING LIMITS if( $upload_Size >2000000) //-------- { unlink($upload_Temp); die("<p align='center'><b><font face='Arial Black' size='2' color='#FF0000'>ERROR<br>upload size > 2mgs <br>PLEASE CHECK THE FILE SIZE AND FORMAT</font></b></p>"); } if( $upload_Mime_Type != "image/cgm" AND $upload_Mime_Type != "image/g3fax" AND $upload_Mime_Type != "image/gif" AND $upload_Mime_Type != "image/ief" AND $upload_Mime_Type != "image/pjpeg" AND $upload_Mime_Type != "image/jpeg" AND $upload_Mime_Type != "image/naplps" AND $upload_Mime_Type != "image/png" AND $upload_Mime_Type != "image/prs.btif" AND $upload_Mime_Type != "image/prs.pti" AND $upload_Mime_Type != "image/tiff" AND $upload_Mime_Type != "image/vnd.cns.inf2" AND $upload_Mime_Type != "image/vnd.dwg" AND $upload_Mime_Type != "image/vnd.dxf" AND $upload_Mime_Type != "image/vnd.fastbidsheet" AND $upload_Mime_Type != "image/vnd.fpx" AND $upload_Mime_Type != "image/vnd.fst" AND $upload_Mime_Type != "image/vnd.fujixerox.edmics-mmr" AND $upload_Mime_Type != "image/vnd.fujixerox.edmics-rlc" AND $upload_Mime_Type != "image/vnd.mix" AND $upload_Mime_Type != "image/vnd.net-fpx" AND $upload_Mime_Type != "image/vnd.svf" AND $upload_Mime_Type != "image/vnd.wap.wbmp" AND $upload_Mime_Type != "image/vnd.xiff" ) { unlink($upload_Temp); die("<p align='center'><b><font face='Arial Black' size='2' color='#FF0000'>ERROR<br>invalid file type<br>PLEASE CHECK THE FILE SIZE AND FORMAT</font></b></p>"); } $uploadFile = "uploads/".$upload_Name ; if (!is_dir(dirname($uploadFile))) { @RecursiveMkdir(dirname($uploadFile)); } else { @chmod(dirname($uploadFile), 0777); } @move_uploaded_file( $upload_Temp , $uploadFile); chmod($uploadFile, 0644); //CHANGE THIS TO THE YOUR DOMAIN $upload_URL = "http://www.AmtrakFamilyReunions.com/uploads/".$upload_Name ; //------------ $pfw_header = "From: $email"; $pfw_subject = "AN IMAGE HAS BEEN UPLOADED"; // CHANGE THIS TO YOUR EMAIL ADDRESS $pfw_email_to = "info@fatcreative.com"; //------------ $pfw_message = "email: $email\n" . "Name: $Name\n" . "story: $story\n" . "upload: $upload_URL\n"; @mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ; echo("<p align='center'><b><font face='Arial Black' size='2' color='#0000FF'>THANK YOU!<br>YOUR IMAGE HAS BEEN UPLOADED</font></b></p>"); include("footer.html"); ?> Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted June 21, 2008 Share Posted June 21, 2008 Technically, the image is never uploaded until after it has been checked. The way you asked the question was as if the image is uploaded and THEN the file type is checked. Is it just giving you an error no matter what? Or can you give an example of the issue you run into? Quote Link to comment Share on other sites More sharing options...
fat creative Posted June 21, 2008 Author Share Posted June 21, 2008 What happens is, with the script unmodified, when I hit upload and do not enter a filename, I get an error message that basically says I did not enter a file to upload (which is one of the checks in the script). When I comment out that section, and hit upload without entering a filename, I get an error message that says I was trying to upload an invalid file type (I was uploading nothing, and that is another check in the script). What I am hoping to do is, if I do not enter a file to upload, that it checks nothing and just emails me the information in the name, email and story box. However, if I do enter a filename to upload, then it will perform the size and file type checks. Quote Link to comment Share on other sites More sharing options...
fat creative Posted June 22, 2008 Author Share Posted June 22, 2008 Well with a bit more putzing around, I figured it out. I'll post the answer in case it helps anyone else. On the statement that checks for file types, I added in AND $upload_Mime_Type != "" so if there was NO filename entered, it would not produce an error. Thank you everyone for your help and suggestions! Jeanette Quote Link to comment 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.