2017-04-24 82 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 '); 
    }