telsiin Posted June 18, 2008 Share Posted June 18, 2008 How can make sure that the file that are upload to my site are a specific format like in this case jpg or can I test for the type of file as if ($_FILES[map1][type] =="jpg")// Or something to this effect without error { if($_FILES[map1] !=" ") { @copy($_FILES[map1][tmp_name],//might have to change tmp_name to tmp\img "C:/lordsofansalon/htdocs/DM/mapsrc/".$_FILES[map1][name])//might have to change to correct path or die("Couldn't copy file please make sure it a jpg and under 2 megs."); } else { die("No Input file specified"); } } else { echo "File must be with jpg format or they will Not be accepted !"; } Link to comment https://forums.phpfreaks.com/topic/110675-upload-file-part-2/ Share on other sites More sharing options...
DarkWater Posted June 18, 2008 Share Posted June 18, 2008 1) Don't use copy(), use move_uploaded_file(). 2) Enclose your array keys in ' ' for proper syntax. Speeds up your script too. 3) You can use substr to get the last 3 or 4 letters of the file name and see if it's the extension you were expecting. Link to comment https://forums.phpfreaks.com/topic/110675-upload-file-part-2/#findComment-567775 Share on other sites More sharing options...
Riparian Posted June 18, 2008 Share Posted June 18, 2008 This is what I use... it might help if ($_FILES['mem_upload']['size'] > 153600 ){ $errs=true; $msg.="Your Upload file was too Big Please Try again with a Smaller File.. "; } // endif file size too big $ext= explode ('.', $_FILES['mem_upload']['name']); $ext[1]=strtolower($ext[1]); if(strtolower($ext[1]) !='png' && strtolower($ext[1]) !='txt' && strtolower($ext[1])!='doc' && strtolower($ext[1])!='pdf' && strtolower($ext[1])!='jpg' && strtolower($ext[1])!='jpeg'){ $errs=true;$msg.='Your Script is not a Valid txt jpg or bmp or png'; } Link to comment https://forums.phpfreaks.com/topic/110675-upload-file-part-2/#findComment-567790 Share on other sites More sharing options...
telsiin Posted June 18, 2008 Author Share Posted June 18, 2008 Riparian: Thank you that worked DarkWater: I also try to do it your way but it didn't work, I was also following some example out of a book I am using xampp on window and don't know if it makes a difference in the code Your help will be greatly appreciated Antonio form_upload.inc <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Upload Maps</title> <style type="text/css"> <!-- .style1 { font-family:"Tempus Sans ITC"; font-size: xx-large; font-weight: bold; } .style3 {font-family: "Tempus Sans ITC"; font-size: medium; font-weight: bold; } body { background-image: url(images/back2.jpg); background-repeat: repeat; } --> </style> </head> <body> <span class="style1">Upload maps in a jpg format</span> <form action="uploadFile.php" method="post" enctype="multipart/form-data"> <span class="style3">Maps to Upload:</span> <input type="hidden" name="max_file_size" value="10000" <input type="file" name="map1" size="50" /><p> <input type="submit" name="Upload" value="Upload Map" /> </form> </body> </html> uploadFile.php <?php /* Srcipt name :uploadfile.php * Description: uploads a file via HTTP with a post form * <span class="style3">Maps to Upload:</span> * <form action="uploadFile.php" method="post" enctype="multipart/form-data"> * <input type="hidden" name="max_file_size" value="10000" * <input type="file" name="map1" size="50" /><p> * <input type="submit" name="Upload" value="Upload Map" /> * </form> */ if (!isset($_POST['Upload'])) { include("form_upload.inc"); } else { if($_FILES['map1']['tmp_name']=="none") { echo"<p style='font-weight:bold'> File did not successfully upload. Check the file size. File must be less then 500K.</p>"; include("form_upload.inc"); exit(); } if(!ereg("image",$_FILES['map1']['type'])) { echo"<p style='font-weight:bold'> File is not picture. please try another file.</p>"; include("form_upload.inc"); exit(); } else { $destination='C:\lordsofansalon\htdocs\DM\mapsrc'."\\".$_FILES['map1']['tmp_name']; //$destination='C:\data'."\\".$_FILES['map1']['tmp_name'];// this may need to be changed! $temp_file= $_FILES['map1']['tmp_name']; move_uploaded_file($temp_file,$destination); echo"<p style='font-weight:bold'> The File was successfully uploaded:{$_FILES['map1']['name']}({$_FILES['map1']['name']})</p>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/110675-upload-file-part-2/#findComment-567834 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.