woolyg Posted August 28, 2007 Share Posted August 28, 2007 Hi all, I am writing a file upload script, but only want the file types to be jpg, gif or png. I want the script to basically say "If this file type is not a jpg, gif or png, then echo a rejection" I'm using (and it's not working) <?php if($fileType != 'jpg', 'gif', 'png'){ echo "Wrong file type"; } else { echo "Right file type"; } ?> Anyone got any idea how to define more than one value into the IF statement, above? All help appreciated, Woolyg. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 28, 2007 Share Posted August 28, 2007 Either: <?php if($fileType != 'jpg' && $fileType != 'gif' && $fileType != 'png'){ echo "Wrong file type"; } else { echo "Right file type"; } ?> Or: <?php $extensions = array('jpg','gif','png'); if(!in_array($fileType,$extensions){ echo "Wrong file type"; } else { echo "Right file type"; } ?> Using an array will make things more manageable. Though it is also likely to be less efficient since you have to use the in_array() function. However, we're talking tiny differences here, so its largely irrelevant. Quote Link to comment Share on other sites More sharing options...
chronister Posted August 28, 2007 Share Posted August 28, 2007 The proper way would be something like this <?php if($_FILES['FIELD_NAME']['type']=='image/gif' || $_FILES['FIELD_NAME']['type'] == 'image/jpeg') // check and see if the file type is an image { echo 'File is a gif or jpg'; } else { echo 'File is not a jpg or gif'; } ?> I believe that a png would be image/png so just add another one there. Keep in mind that if the file upload field is blank, then it does not meet the criteria, so you may want to add a $_FILES['FIELD_NAME']['type']==''; part in there too. Nate Quote Link to comment Share on other sites More sharing options...
ReDucTor Posted August 28, 2007 Share Posted August 28, 2007 I would highly suggest not relying on mime type sent from the browser, as not all browsers will send the mime type, and its easy to send say a PHP file as image/jpeg mime type. I suggest sticking to file extensions, and you could also use exif_imagetype() or getimagesize() if you really want to determine if its an image and valid. Quote Link to comment Share on other sites More sharing options...
woolyg Posted August 28, 2007 Author Share Posted August 28, 2007 Thanks for the input all, it's great that there are multiple ways of doing this. Thanks for the pointer on mime types too! Cheers, Woolyg. Quote Link to comment Share on other sites More sharing options...
chronister Posted August 28, 2007 Share Posted August 28, 2007 @ReDucTor -- I did not know that relying on the mime types are not a "great" way of doing it. Can you give an example of how to forge the mime type. I use this in several scripts. Thanks Nate 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.