hash1 Posted November 24, 2009 Share Posted November 24, 2009 Hey guys im working on my first script that will actually be useful to people, however im wondering how I can only allow users to upload the following file formats: .zip, .rar, and .7zip Here is the code that takes care of that, but I feel I have done it incorrectly. if ((($_FILES["file"]["type"] == ".zip") || ($_FILES["file"]["type"] == ".rar") || ($_FILES["file"]["type"] == ".7zip")) && ($_FILES["file"]["size"] < 20000)) { I also want to change the maximum file size to something in the MB range instead of 20 kilobytes also but am unsure how to. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/182818-help-with-upload-file-types-allowed/ Share on other sites More sharing options...
flyhoney Posted November 24, 2009 Share Posted November 24, 2009 To change the maximum upload size you have to edit your php.ini. I think the parameter you want to edit is post_max_size or something like that. As for checking the file type, you should just do: print_r($_FILES); And see what it's spitting out, and then you can make sure you're checking the right thing. Quote Link to comment https://forums.phpfreaks.com/topic/182818-help-with-upload-file-types-allowed/#findComment-964926 Share on other sites More sharing options...
jcombs_31 Posted November 24, 2009 Share Posted November 24, 2009 The type is the file mime-type, not the extension. Quote Link to comment https://forums.phpfreaks.com/topic/182818-help-with-upload-file-types-allowed/#findComment-964944 Share on other sites More sharing options...
mikesta707 Posted November 24, 2009 Share Posted November 24, 2009 I think he means he wants to check the file type in the backend, not show the whole structure of the array. as jcombs said, the type attribute of files is the mime type not the extension mime type wiki article if you want the extension, you can do something like $extension = end(explode('.', $filename)); but the way you are checking can be slightly improved (not really for efficiency, but it will look cleaner) you can use in_array(), and make an array of allowed types, IE $allowed = array("jpeg", "html", "etc"); //check if the file type is right if (in_array($_FILES['file']['type'], $allowed)){ //continue } else { //exit } Quote Link to comment https://forums.phpfreaks.com/topic/182818-help-with-upload-file-types-allowed/#findComment-964948 Share on other sites More sharing options...
mrMarcus Posted November 24, 2009 Share Posted November 24, 2009 I think he means he wants to check the file type in the backend, not show the whole structure of the array. as jcombs said, the type attribute of files is the mime type not the extension mime type wiki article if you want the extension, you can do something like $extension = end(explode('.', $filename)); but the way you are checking can be slightly improved (not really for efficiency, but it will look cleaner) you can use in_array(), and make an array of allowed types, IE $allowed = array("jpeg", "html", "etc"); //check if the file type is right if (in_array($_FILES['file']['type'], $allowed)){ //continue } else { //exit } keep in mind OP (as i'm sure it was just an oversight by mikesta), checking $_FILES['file']['type'] against the $allowed array will return false. you can use mikesta's $extension to check against the array: <?php if (in_array ($extension, $allowed)) ?> or you will have to change the values in the $allowed array: <?php $allowed = array ('image/jpeg', 'text/html'); if (in_array ($_FILES['file']['type'], $allowed)) //will now return true if a jpeg or .html file is uploaded; ?> Quote Link to comment https://forums.phpfreaks.com/topic/182818-help-with-upload-file-types-allowed/#findComment-964963 Share on other sites More sharing options...
mikesta707 Posted November 24, 2009 Share Posted November 24, 2009 yeah, sorry I got lazy and didn't feel like looking up the mime types, so I just kind of put the extension. thanks for clearing that up Quote Link to comment https://forums.phpfreaks.com/topic/182818-help-with-upload-file-types-allowed/#findComment-964965 Share on other sites More sharing options...
mrMarcus Posted November 24, 2009 Share Posted November 24, 2009 yeah, sorry I got lazy and didn't feel like looking up the mime types, so I just kind of put the extension. thanks for clearing that up no worries. wasn't for you, was for the OP. as i said, simple oversight. Quote Link to comment https://forums.phpfreaks.com/topic/182818-help-with-upload-file-types-allowed/#findComment-964977 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.