MoFish Posted May 30, 2006 Share Posted May 30, 2006 hello, can anyone identify why my upload code wont work? im catching the the error saying the filetype is not allowed however have tryed both jpeg and gif with no success.PS: is this the correct way to validate the file type?[code] <?php } else { if ($_FILES['file']['type'] != "image/gif" AND $_FILES['file']['type'] != "image/jpg") { $error = "This file type is not allowed"; unlink($_FILES['file']['tmp_name']); // assign error message, remove uploaded file, redisplay form. } else {[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10742-upload-image-script-slight-problem/ Share on other sites More sharing options...
ldsmike88 Posted May 30, 2006 Share Posted May 30, 2006 For jpeg it says image/pjpeg. It should probably say image/jpg. If that doesn't work I would say take out the image/ and just leave the jpg to see if that would work.Most people don't like reading through someone's whole script to find their error. Next time I would recomend you just post the part where you think the error is.Hope this helps! Quote Link to comment https://forums.phpfreaks.com/topic/10742-upload-image-script-slight-problem/#findComment-40151 Share on other sites More sharing options...
MoFish Posted May 30, 2006 Author Share Posted May 30, 2006 thanks for the reply, I tryed the following but am still getting the error saying this file type is not allowed. :( Quote Link to comment https://forums.phpfreaks.com/topic/10742-upload-image-script-slight-problem/#findComment-40214 Share on other sites More sharing options...
MoFish Posted May 30, 2006 Author Share Posted May 30, 2006 I also tryed the following, using an array. But this doesnt work either.[code]$types_array = array('image/gif','image/jpg','image/x-png'); if(!in_array($_FILES['file']['type'], $types_array)){ $error = "This file type is not allowed"; unlink($_FILES['file']['tmp_name']); } else {[/code]whole script below[code]<?php#################################### assign variables####################################$error = "";$maxfilesize=100240;$types_array = array('image/gif','image/pjpeg','image/x-png');if (isset($_POST['submit'])){#################################### if there is a file name > proceed#################################### if (!is_uploaded_file($_FILES['file']['tmp_name'])) { $error = "You did not upload a file!"; unlink($_FILES['file']['tmp_name']); } else {##################################### validate max file size #################################### if ($_FILES['file']['size'] > $maxfilesize) { $error = "file is too large"; unlink($_FILES['file']['tmp_name']); } else { ##################################### validate file type #################################### if(!in_array($_FILES['file']['type'], $types_array)){ $error = "This file type is not allowed"; unlink($_FILES['file']['tmp_name']); } else {##################################### finally, copy file to destination#################################### copy($_FILES['file']['tmp_name'],"/uploads/".$_FILES['file']['name']); unlink($_FILES['file']['tmp_name']); print "File has been successfully uploaded!"; exit; } } }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10742-upload-image-script-slight-problem/#findComment-40221 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.