File Validation

Here is a simple function to valid a image file. You can modify/add extensions and mime type according to your needs.

      
   function checkImageTypeArray($file)
   {
        $errorarray=array();
        $allowExtension = array("gif", "jpeg", "jpg", "png");
		$allowMime = array("image/gif", "image/jpesg", "image/pjpeg", "image/x-png","image/png");
        $getFileExtension = pathinfo($file['name'],PATHINFO_EXTENSION);
        if (
        !in_array($file['type'], $allowMime)
        || !in_array($getFileExtension, $allowExtension)
        || $file["size"] > 400000
        || $file['error']!='0'		
		) {
              $errorarray[]=$file["name"]." is a invalid file for the field.
"; } return $errorarray; }

How to use this?

Here is a complete example :

   function checkImageTypeArray($file)
   {
        $errorarray=array();
        $allowExtension = array("gif", "jpeg", "jpg", "png");
		$allowMime = array("image/gif", "image/jpesg", "image/pjpeg", "image/x-png","image/png");
        $getFileExtension = pathinfo($file['name'],PATHINFO_EXTENSION);
        if (
        !in_array($file['type'], $allowMime)
        || !in_array($getFileExtension, $allowExtension)
        || $file["size"] > 400000
        || $file['error']!='0'		
		) {
              $errorarray[]=$file["name"]." is a invalid file for the field.
"; } return $errorarray; } if(isset($_POST['submitBtn'])) { print_r($_FILES); $checkFile=checkImageTypeArray($_FILES['image']); if(!empty($checkFile)) { print_r($checkFile); } else{ echo "File is valid"; } }