postbil.com Posted October 30, 2009 Share Posted October 30, 2009 Hello Phpfreaks.. I have a little problem with a validating of different file types. At my site I had made an script there make its possible for the user to upload their own picture. But now I need a way to ensure that it just JPEG and GIF files the user can upload.. I had reed the manual but I don’t find something useful.. Are there somebody there can / will help me? Thanks a lot!! Postbil.com Quote Link to comment https://forums.phpfreaks.com/topic/179624-validating-flie-types/ Share on other sites More sharing options...
Bricktop Posted October 30, 2009 Share Posted October 30, 2009 Hi postbil.com, Post your code for an accurate answer. Quote Link to comment https://forums.phpfreaks.com/topic/179624-validating-flie-types/#findComment-947782 Share on other sites More sharing options...
AaronC Posted October 30, 2009 Share Posted October 30, 2009 This seems to be a pretty easy tutorial to understand. http://www.reconn.us/file_uploading.html Quote Link to comment https://forums.phpfreaks.com/topic/179624-validating-flie-types/#findComment-947785 Share on other sites More sharing options...
postbil.com Posted October 30, 2009 Author Share Posted October 30, 2009 Here at the code if it can be a help.. <?php if ($_FILES['minfil']) //Har brugeren forsøgt at uploade noget? { //Bestem hvor filen skal smides hen og og hvad den skal hedde $destination = "Image/" . $_FILES['minfil']['name']; //Forsøg at flyttede den uploadede fil har dens midlertidige destination til den nye if (move_uploaded_file($_FILES['minfil']['tmp_name'], $destination)) { echo "Filen" . $_FILES['minfil']['name'] . " blevet uploadet"; } else { echo "Der er sket en fejl"; } } ?> <form action="eks3.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="100000"> Vælg fil: <input name="minfil" type="file"> <input type="submit" value="Upload fil"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/179624-validating-flie-types/#findComment-947797 Share on other sites More sharing options...
Bricktop Posted October 30, 2009 Share Posted October 30, 2009 Hi postbil.com, I've added a simple if statement to your code to check the filetype being uploaded. If it's .JPG or .GIF the script will continue. If not, it will echo an error message. <?php if ($_FILES['minfil']) //Har brugeren forsøgt at uploade noget? { //Perform a check to see which filetype has been uploaded. If it's a .JPG or a .GIF continue. if( $_FILES["minfil"]["type"] == "image/pjpeg" || $_FILES["minfil"]["type"] == "image/jpeg" || $_FILES["minfil"]["type"] == "image/gif" ) { //Bestem hvor filen skal smides hen og og hvad den skal hedde $destination = "Image/" . $_FILES['minfil']['name']; //Forsøg at flyttede den uploadede fil har dens midlertidige destination til den nye if (move_uploaded_file($_FILES['minfil']['tmp_name'], $destination)) { echo "Filen" . $_FILES['minfil']['name'] . " blevet uploadet"; } else { echo "Der er sket en fejl"; } } //If the filetype being uploaded is not a .JPG or a .GIF then echo the error below. else { echo "Please upload a valid filetype. Valid filetypes are .JPG and .GIF"; } } ?> <form action="eks3.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="100000"> Vælg fil: <input name="minfil" type="file"> <input type="submit" value="Upload fil"> </form> Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/179624-validating-flie-types/#findComment-947800 Share on other sites More sharing options...
postbil.com Posted October 30, 2009 Author Share Posted October 30, 2009 Yes... it worked great many thanks for your help .. Quote Link to comment https://forums.phpfreaks.com/topic/179624-validating-flie-types/#findComment-947884 Share on other sites More sharing options...
mikesta707 Posted October 30, 2009 Share Posted October 30, 2009 By the way, using mime-types is not the best way. Mime types can be easily spoofed, and some browsers don't even send them. Some even send different ones for specific types. I usually restrict file type like so $allowed = array("jpg", "jpeg", "gif");//make an array for allowed file types $ext = end(explode('.', $_FILES['minfile']['name']));//get the extension of the file //check it if (!in_array($ext, $allowed)){//its not an allowed file type echo "File type not accepted."; exit(); } //continue with upload Quote Link to comment https://forums.phpfreaks.com/topic/179624-validating-flie-types/#findComment-947890 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.