我用这段代码来检查图像的类型,
$f_type=$_FILES['fupload']['type'];
if ($f_type== "image/gif" OR $f_type== "image/png" OR $f_type== "image/jpeg" OR $f_type== "image/JPEG" OR $f_type== "image/PNG" OR $f_type== "image/GIF")
{
$error=False;
}
else
{
$error=True;
}
但一些用户抱怨说,他们在上传任何类型的图片时都会出错,而另一些用户则没有得到任何错误!
我想知道这是否解决了这个问题:
if (mime_content_type($_FILES['fupload']['type']) == "image/gif"){...
有什么意见吗?
发布于 2011-07-20 07:18:07
切勿使用$_FILES..['type']
。它包含的信息根本没有被验证,它是一个用户定义的值。自己测试这个类型。对于图像,exif_imagetype
通常是一个很好的选择:
$allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
$detectedType = exif_imagetype($_FILES['fupload']['tmp_name']);
$error = !in_array($detectedType, $allowedTypes);
或者,如果您的服务器支持finfo
functions,那么它们是很好的。
发布于 2011-07-20 07:23:05
除了@deceze之外,您还可以使用finfo()检查非镜像文件的MIME类型:
$finfo = new finfo();
$fileMimeType = $finfo->file($path . $filename, FILEINFO_MIME_TYPE);
发布于 2017-01-05 16:29:16
当然你可以用exif检查它是否是一个图像,但我认为更好的方法是用这样的finfo:
$allowed_types = array ( 'application/pdf', 'image/jpeg', 'image/png' );
$fileInfo = finfo_open(FILEINFO_MIME_TYPE);
$detected_type = finfo_file( $fileInfo, $_FILES['datei']['tmp_name'] );
if ( !in_array($detected_type, $allowed_types) ) {
die ( 'Please upload a pdf or an image ' );
}
finfo_close( $fileInfo );
https://stackoverflow.com/questions/6755192
复制相似问题