ChrisMayhew Posted July 21, 2011 Share Posted July 21, 2011 Hey I am working on an image upload for my site but I am having an issue at the moment, I am allowing multiple files from the one input box which works fine, it uploads all the files I select and I can echo out each files information on the next page. However I want to filter file types by images but I am having an issue. I found an example of using in_array to check the file type against the acceptable file types that are stored in array. Function UploadFiles($imgs, $UserID, $DT, $IP) { //Setup the variables. $MaxFileSize = 6291456; $Allowed_FileTypes = array("image/jpeg","image/jpg","image/gif","image/bmp","image/png"); $FileCount = count($_FILES['imgs']); //Now the uploads have been submitted and uploaded properly we can process them. for ($i = 0; $i < $FileCount; $i++) { $FileName = $_FILES['imgs']['name'][$i]; $FileSize = $_FILES['imgs']['size'][$i]; $FileType = $_FILES['imgs']['type'][$i]; if(in_array($FileType, $Allowed_FileTypes)) { $Result = "Uploaded"; } else { $Result = "<div id='error1'>Error: That File Type isn't supported, You can only upload JPG, PNG, GIF and BMP files.</div><br />"; } } return $Result; } Now if I echo the $FileType out it displays it as image/jpeg etc but it always displays the error. Any ideas or will this just not work this way? Quote Link to comment https://forums.phpfreaks.com/topic/242530-multiple-file-upload-file-type-problem/ Share on other sites More sharing options...
Porl123 Posted July 21, 2011 Share Posted July 21, 2011 Could you post the html for the upload form? Quote Link to comment https://forums.phpfreaks.com/topic/242530-multiple-file-upload-file-type-problem/#findComment-1245570 Share on other sites More sharing options...
ChrisMayhew Posted July 21, 2011 Author Share Posted July 21, 2011 Yes sorry, should have put that in my first post as well. <form action="./upload.php" method="post" enctype="multipart/form-data" class='UploadForm'> <label for='imgs'>Image(s):</label> <input type='file' name='imgs[]' multiple /> <br /> <input type='submit' name='Upload' value='Upload' /></form> It's nice and simple for now. That was the example I found on the net showing how to do multiple files, like I said it seems to be uploading them fine because I can echo out the $FileType or $FileName and it outputs the correct information. Quote Link to comment https://forums.phpfreaks.com/topic/242530-multiple-file-upload-file-type-problem/#findComment-1245598 Share on other sites More sharing options...
dcro2 Posted July 21, 2011 Share Posted July 21, 2011 $FileCount = count($_FILES['imgs']); This will always be 5 if two or more files are uploaded. You'll want to count $_FILES['imgs']['name'] or some other member. Can't figure out why in_array might not be matching though... Quote Link to comment https://forums.phpfreaks.com/topic/242530-multiple-file-upload-file-type-problem/#findComment-1245604 Share on other sites More sharing options...
PFMaBiSmAd Posted July 21, 2011 Share Posted July 21, 2011 You MUST test for errors before you can access ANY of the uploaded file information. See Example #3 at the following link for how you can iterate over any number of multiple uploaded files - http://is.php.net/manual/en/features.file-upload.post-method.php You would put your code that accesses the uploaded file information inside the if ($error == UPLOAD_ERR_OK) { code to access the successful uploaded file information } statement. Also, when validating user supplied information, you should tell the user in the error message that you output exactly what was wrong with the data he supplied. You should echo the $FileType value as part of your error message so that you have debugging information during testing (different browsers and browser versions supply different type values for the same file) and a user has more information about what he actually submitted that was incorrect. Quote Link to comment https://forums.phpfreaks.com/topic/242530-multiple-file-upload-file-type-problem/#findComment-1245632 Share on other sites More sharing options...
ChrisMayhew Posted July 21, 2011 Author Share Posted July 21, 2011 $FileCount = count($_FILES['imgs']); This will always be 5 if two or more files are uploaded. You'll want to count $_FILES['imgs']['name'] or some other member. Can't figure out why in_array might not be matching though... The count always being 5 was actually the problem Of course it checked the first one which was actually an image and that was correct and matched, but because I wasn't storing each error message up it was checking the other 4 which thrown errors. Counting it by the file names makes it work as it should. You MUST test for errors before you can access ANY of the uploaded file information. See Example #3 at the following link for how you can iterate over any number of multiple uploaded files - http://is.php.net/manual/en/features.file-upload.post-method.php You would put your code that accesses the uploaded file information inside the if ($error == UPLOAD_ERR_OK) { code to access the successful uploaded file information } statement. Also, when validating user supplied information, you should tell the user in the error message that you output exactly what was wrong with the data he supplied. You should echo the $FileType value as part of your error message so that you have debugging information during testing (different browsers and browser versions supply different type values for the same file) and a user has more information about what he actually submitted that was incorrect. Thanks for the tips, I will make sure I check for errors with the images as well. Quote Link to comment https://forums.phpfreaks.com/topic/242530-multiple-file-upload-file-type-problem/#findComment-1245655 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.