LanceT Posted February 1, 2007 Share Posted February 1, 2007 How do I use PHP to upload only one file type and return errors for all other file type? For example, I want to limit my users to only JPG, GIF, and PNG files, how do I do this? Link to comment https://forums.phpfreaks.com/topic/36590-uploading-only-one-file-type/ Share on other sites More sharing options...
dgiberson Posted February 1, 2007 Share Posted February 1, 2007 you need to check the MIME type or $_FILES['type'] variable, create an array to hold the allowed types and then you can compare the upload against the array. there are additional checks you can perform, but this is the most basic. Here is a basic tutorial i found: http://php.about.com/od/advancedphp/ss/php_file_upload_2.htm Here is a link for MIME types: http://www.ltsw.se/knbase/internet/mime.htp Link to comment https://forums.phpfreaks.com/topic/36590-uploading-only-one-file-type/#findComment-174289 Share on other sites More sharing options...
redarrow Posted February 1, 2007 Share Posted February 1, 2007 somethink like this ok. <?php $target = "upload/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $_FILES['type']=array(".jpg",".png",".gif"); foreach($_FILES['type'] as $wanted_file_type){ if(!$wanted_file_type){ echo "sorry you must only use the following file type <br> .jpg <br> .png <br> .gif <br> please try agin"; } } $ok=1; if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else { echo "Sorry, there was a problem uploading your file."; } ?> Link to comment https://forums.phpfreaks.com/topic/36590-uploading-only-one-file-type/#findComment-174320 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.