File extension in_array()

Hi,

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(".", 
            		strtolower($file['name']))), 
            		$allowedExtensions)) { 
       		$error_msg = 'File type not allowed'; 
      		} 
    	} 
  	} 

What am I doing wrong?

Thanks

That is a bad way to enforce file type.

Use or:
http://us.php.net/manual/en/function.getimagesize.php
http://us.php.net/manual/en/function.exif-imagetype.php

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

//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);
if(!in_array($file_ext,$allowedExtensions)){
$error_msg = ‘File ‘.$file_name.’ is not allowed’;
}
}

if(count($error_msg)>0){
foreach($error_msg as $errors){
echo $errors.“<br/>”;
}
}
?>