PHP如何使用getimagesize()来检查上传的图像类型 [英] php how to use getimagesize() to check image type on upload
问题描述
可能重复:
GetImageSize()不返回FALSE,当它应该
我目前有一个过滤系统如下:
//检查上传文件的类型是否有效图像类型
函数is_valid_type($文件)
{
//这是一个包含所有有效图像MIME类型的数组
$ valid_types = array(image / jpg, image / JPG,image / jpeg,image / bmp,image / gif,image / png);
if(in_array($ file ['type'],$ valid_types))
return 1;
返回0;
}
但是我被告知最好是自己检查文件类型,我会用getimagesize()来检查文件类型吗?
返回一个包含7个元素的数组。数组的索引2包含指示图像类型的 IMAGETYPE_XXX
常量之一。 相当于使用getimagesize()提供的函数将是
pre $函数is_valid_type($ file)
{
$ size = getimagesize($文件);
if(!$ size){
return 0;
}
$ valid_types = array(IMAGETYPE_GIF,IMAGETYPE_JPEG,IMAGETYPE_PNG,IMAGETYPE_BMP);
if(in_array($ size [2],$ valid_types)){
return 1;
} else {
return 0;
}
}
Possible Duplicate:
GetImageSize() not returning FALSE when it should
i currently have a filter system as follows:
// Check to see if the type of file uploaded is a valid image type
function is_valid_type($file)
{
// This is an array that holds all the valid image MIME types
$valid_types = array("image/jpg", "image/JPG", "image/jpeg", "image/bmp", "image/gif", "image/png");
if (in_array($file['type'], $valid_types))
return 1;
return 0;
}
but i have been told that it is better to check the filetype myself, how would i use the getimagesize() to check the filetype in a similar way?
getimagesize()
returns an array with 7 elements. The index 2 of the array contains one of the IMAGETYPE_XXX
constants indicating the type of the image.
The equivalent of the function provided using getimagesize() would be
function is_valid_type($file)
{
$size = getimagesize($file);
if(!$size) {
return 0;
}
$valid_types = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP);
if(in_array($size[2], $valid_types)) {
return 1;
} else {
return 0;
}
}
这篇关于PHP如何使用getimagesize()来检查上传的图像类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!