File extension in_array()


I have 5 upload file fields… I need it to check through the 5 files and make sure that they are all .jpg files… If any of the 5 files are different extensions, then populate $error_msg.

Been trying with code bellow, but it seems to let the form submit the files are mixed say jpg and png…

	$allowedExtensions = array("jpg","jpeg","pjpeg"); 
  		foreach ($_FILES['photoname'] as $file) { 
    		if ($file['tmp_name'] > '') { 
      		if (!in_array(end(explode(".", 
            		$allowedExtensions)) { 
       		$error_msg = 'File type not allowed'; 

What am I doing wrong?


That is a bad way to enforce file type.

Use or:

Should something like

	$types_array = array("image/jpeg","image/pjpeg");
	if (!in_array($_FILES['photoname']['type'], $types_array)) {
	$error_msg = 'Invalid File type';


I tried a few ways using exif_imagetype() But its not working out I guess because its not a single file being sent?

You should not be using “type” from $_FILES either…

Use following code to validate exact

<!-- your file fields are–>
<input type=“file” name=“photoname” id=“file1”/>
<input type=“file” name=“photoname” id=“file2”/>
<input type=“file” name=“photoname” id=“file3”/>
<input type=“file” name=“photoname” id=“file4”/>

//php code to validate
$allowedExtensions = array(“jpg”,“jpeg”,“pjpeg”);
$error_msg = array();
foreach ($_FILES[‘photoname’] as $file) {
$file_name = $file[‘name’];
$file_ext = substr($file_name, strrpos($file_name, ‘.’)+1);
$error_msg = ‘File ‘.$file_name.’ is not allowed’;

foreach($error_msg as $errors){
echo $errors.“<br/>”;