2017-04-24 79 views
0

我试图检查上传的图像是PNG,JPG还是GIF,而不仅仅是检查文件扩展名。我正在尝试以下操作:PHP - 使用exif_imagetype错误检查文件类型

$allowed_types = array (IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF); 
$detectedType = exif_imagetype($_FILES['file_to_upload']['tmp_name']); 
    if (!in_array($detectedType, $allowed_types)) { 
     die ('Please upload a pdf or an image '); 
    } 


//code to handle image 

但是,即使它是图像,我也会收到警报。任何人都可以指出我为什么?

+0

为什么downvote没有任何评论? – spbrad

+0

[php check file extension in upload form]可能重复(http://stackoverflow.com/questions/10456113/php-check-file-extension-in-upload-form) – shaggy

+0

@shaggy我试图找出从exif数据扩展,而不是通过声明数组中允许的扩展名,所以不重复 – spbrad

回答

0

应该是:

$allowed_types = array (IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF); 
$detectedType = exif_imagetype($_FILES['file']['tmp_name']); 
    if (!in_array($detectedType, $allowed_types)) { 
    die ('Please upload a pdf or an image '); 
    }