[英]php image validation with zip validation
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 我正在创建PHP图像验证和zip文件验证脚本,我的zip文件验证脚本正在运行,但是我添加了图像验证,但是它不起作用,我想添加图像文件类型jpeg,png文件类型,我试图这样做是行不通的
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);
I would first check the temp file with mime_content_type. 我将首先使用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 . '".');
}
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.