0

I am creating PHP image validation and zip file validation scripts my zip file validation script is working but I have added image validation but it's not working I want to add image file type jpeg,png file type I have tried to do that's not working

Here is my code

if($_FILES['fileImage']['tmp_name'][0] != "fileImage/jpg") {
die($_FILES['fileImage']['name'][0] . " is not a valid image file.");
exit;
}

$z = zip_open($_FILES['fileImage']['tmp_name'][4]);
if (!is_resource($z)) {
    die($_FILES['fileImage']['name'][4] . " is not a valid ZIP file.");
}
zip_close($z);

2 Answers2

0

I would first check the temp file with mime_content_type. The result does not depend on the file extension, which would be easy to manipulate. Further processing could then complete the validation.

$tmpFile = $_FILES['fileImage']['tmp_name'];
switch ($mimeType = mime_content_type($tmpFile)) {
  case 'application/zip':
    //... process zip ... zip_open, etc
    break;
  case 'image/jpeg':
    //... process image ... getimagesize etc.
    break;
  default:
    die('Unknown filetype "' . $mimeType . '".');
}

  • error message coming like this Fatal error: Call to undefined function mime_content_type() in C:\xampp\htdocs\zblog\upload-source.php on line 242 –  Jan 29 '19 at 22:58
  • On Windows you would need to enable the extension first, edit your php.ini uncommenting and pointing mime_magic.magicfile to the right filename containing the mimetypes, add or uncomment extension=php_fileinfo.dll and uncomment the line in httpd.conf containing "LoadModule ... mod_mime_magic.so". – Dolph Roger Jan 29 '19 at 23:27
0

Try the following code.

$tempFile = $_FILES['file']['name'];

// Get the file extension
$fileExt = pathinfo($tempFile, PATHINFO_EXTENSION);

// Allowed extensions
$imageExt = ['jpg', 'png', 'gif'];
$compressExt = ['rar', 'zip'];

// Validation
if ( !in_array($fileExt, $imageExt) ) {
  die('Not image format');
} elseif ( !in_array($fileExt, $compressExt) ) {
  die('No compression format');
} else {
  // proceed
}
tonoslfx
  • 3,422
  • 15
  • 65
  • 107
  • error coming like this Warning: pathinfo() expects parameter 1 to be string, array given in C:\xampp\htdocs\zblog\upload-source.php on line 245 Not image format –  Jan 30 '19 at 16:32