bob_the _builder Posted September 10, 2012 Share Posted September 10, 2012 I have a form with 5 file fields.. if any of those fields contain a file type thats no allowed, the populate $error_msg, if all the files are correct file types, them leave $error_msg empty.. I have been trying with the likes of below, but it apears to stop all file uploads even when all the files are jpg.. $types_array = array("image/jpeg","image/pjpeg"); if (!in_array($_FILES['photoname']['type'], $types_array)) { $error_msg = 'Invalid File type'; } ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/268199-file-extension-in_array/ Share on other sites More sharing options...
premiso Posted September 10, 2012 Share Posted September 10, 2012 Echo out $_FILES['photoname']['type'] and see what prints and it should help you figure out where it is being held up at. Quote Link to comment https://forums.phpfreaks.com/topic/268199-file-extension-in_array/#findComment-1376605 Share on other sites More sharing options...
requinix Posted September 10, 2012 Share Posted September 10, 2012 The [type] in $_FILES is not secure. You can't trust it to tell you what type of file was uploaded. For images, use getimagesize() on the [tmp_name]. If it errors then the file isn't a recognizable image, otherwise it'll tell you anything you need to know about it - including the type of image. Quote Link to comment https://forums.phpfreaks.com/topic/268199-file-extension-in_array/#findComment-1376616 Share on other sites More sharing options...
bob_the _builder Posted September 10, 2012 Author Share Posted September 10, 2012 I know there is this method.. if (exif_imagetype('image.gif') != IMAGETYPE_JPEG) { echo 'The photo type is not allowed'; } But I cant get it to run through the array for uploaded files.. Quote Link to comment https://forums.phpfreaks.com/topic/268199-file-extension-in_array/#findComment-1376618 Share on other sites More sharing options...
premiso Posted September 10, 2012 Share Posted September 10, 2012 $types_array = array(IMAGETYPE_JPEG, IMAGETYPE_ANOTHERIMAGE); That is simple, replace your array code with the above (change the ANOTHERIMAGE to a valid type) and then: if (!in_array(exif_imagetype('image.gif'), $types_array)) { Quote Link to comment https://forums.phpfreaks.com/topic/268199-file-extension-in_array/#findComment-1376619 Share on other sites More sharing options...
bob_the _builder Posted September 10, 2012 Author Share Posted September 10, 2012 Yea that side of it is ok... its checking the 5 files that were sent $_FILES['photoname'].. if there are any that arnt jpegs, then populate the error message.. Quote Link to comment https://forums.phpfreaks.com/topic/268199-file-extension-in_array/#findComment-1376623 Share on other sites More sharing options...
premiso Posted September 10, 2012 Share Posted September 10, 2012 its checking the 5 files that were sent $_FILES['photoname'].. I see, I guess I was mislead by the code you showed, as you seemed to only try 1 file. Do you have more code to provide so I can more accurately assist you with what you want to achieve? Namely the loop portion of the code that iterates over the Photos. Quote Link to comment https://forums.phpfreaks.com/topic/268199-file-extension-in_array/#findComment-1376752 Share on other sites More sharing options...
bob_the _builder Posted September 10, 2012 Author Share Posted September 10, 2012 A basic cut down version of what I am trying to acheive.. if(isset($submit)){ if(any_of_the_5_photos_are_not_jpeg) { $error_msg = 'Only jpg files allowed'; } // Insert record } // show form again if error message is not null if (($error_msg == '') && (isset($submit))){ <form action="'.$_SERVER['PHP_SELF'].'" method="post" enctype="multipart/form-data"> $number_of_fields = 5; $counter = 1; while($counter <= $number_of_fields){ echo '<input name="photoname[]" type="file" id="upload" />'; $counter++; } echo '<input type="submit" name="submit" id="submit" value="submit" />'; </form> } Thanks Quote Link to comment https://forums.phpfreaks.com/topic/268199-file-extension-in_array/#findComment-1376821 Share on other sites More sharing options...
bob_the _builder Posted September 10, 2012 Author Share Posted September 10, 2012 ops I screwed up the code above and cant edit it.. But you get the idea.. Quote Link to comment https://forums.phpfreaks.com/topic/268199-file-extension-in_array/#findComment-1376831 Share on other sites More sharing options...
premiso Posted September 10, 2012 Share Posted September 10, 2012 <?php //... code above this $types_array = array(IMAGETYPE_JPEG, IMAGETYPE_ANOTHERIMAGE); $badImg = array(); foreach ($_FILES['photoname']['tmp_name'] as $key => $iData) { if (!in_array(exif_imagetype($iData), $type_array)) { $badImg[] = $key; } } if (empty($badImg)) { // All files passed. } else { // there was an error and ids are in $badImg } Something like that should get you going. Quote Link to comment https://forums.phpfreaks.com/topic/268199-file-extension-in_array/#findComment-1376837 Share on other sites More sharing options...
Psycho Posted September 10, 2012 Share Posted September 10, 2012 I think is what is being missed here is that there are apparently multiple file input fields. I would assume that not all of them are required. But, the post/file data will include records for all the fields - even if they have no data. So, the logic needs to exclude fields where a file was not supplied. My take: $valid_types = array(IMAGETYPE_JPEG); //Check if any files were submitted if(isset($_FILES['photoname'])) { //Set flag variable $allFilesValid = true; //Iterate through all files submitted foreach($_FILES['photoname']['tmp_name'] as $tmp_file) { //If any "submitted" file is not valid set flag to false and exit loop if($tmp_file != '' && !in_array(exif_imagetype($tmp_file), $valid_types)) { $allFilesValid = false; break; } } } Quote Link to comment https://forums.phpfreaks.com/topic/268199-file-extension-in_array/#findComment-1376842 Share on other sites More sharing options...
bob_the _builder Posted September 11, 2012 Author Share Posted September 11, 2012 Hey, All file fields must hold files to submit.. I use the following to make sure all 5 file fields are populated.. if (array_sum($_FILES['photoname']['error']) > 0) { $error_msg = 'You must upload 5 logos to continue'; } Thanks[/code] Quote Link to comment https://forums.phpfreaks.com/topic/268199-file-extension-in_array/#findComment-1376898 Share on other sites More sharing options...
bob_the _builder Posted September 11, 2012 Author Share Posted September 11, 2012 I tested the following code: $types_array = array(IMAGETYPE_JPEG, IMAGETYPE_ANOTHERIMAGE); $badImg = array(); foreach ($_FILES['photoname']['tmp_name'] as $key => $iData) { if (!in_array(exif_imagetype($iData), $type_array)) { $badImg[] = $key; } } if (empty($badImg)) { // All files passed. } else { // there was an error and ids are in $badImg } Got the following error, which is the error I got no matter what I tried before posting.. Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/###/public_html/includes/forms.php(2) : eval()'d code(1) : eval()'d code (1) : eval()'d code on line 35 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/268199-file-extension-in_array/#findComment-1376908 Share on other sites More sharing options...
requinix Posted September 11, 2012 Share Posted September 11, 2012 The array is named "types_array" but you passed "type_array". Quote Link to comment https://forums.phpfreaks.com/topic/268199-file-extension-in_array/#findComment-1376911 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.