問題描述
我用這段代碼來檢查圖像的類型,
I used this code to check for the type of images,
$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;
}
但有些用戶抱怨他們?cè)谏蟼魅魏晤愋偷膱D像時(shí)出錯(cuò),而有些用戶則沒有出錯(cuò)!
but some users complain they get an error while uploading any type of images, while some others don't get any errors!
我想知道這是否能解決問題:
I was wondering if this fixes the problem:
if (mime_content_type($_FILES['fupload']['type']) == "image/gif"){...
有什么意見嗎?
推薦答案
永遠(yuǎn)不要使用 $_FILES..['type']
.其中包含的信息根本沒有經(jīng)過驗(yàn)證,它是用戶定義的值.自己測(cè)試類型.對(duì)于圖片,exif_imagetype
通常是一個(gè)不錯(cuò)的選擇選擇:
Never use $_FILES..['type']
. The information contained in it is not verified at all, it's a user-defined value. Test the type yourself. For images, exif_imagetype
is usually a good choice:
$allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
$detectedType = exif_imagetype($_FILES['fupload']['tmp_name']);
$error = !in_array($detectedType, $allowedTypes);
或者,finfo
函數(shù) 也很棒,如果您的服務(wù)器支持它們.
Alternatively, the finfo
functions are great, if your server supports them.
這篇關(guān)于如何在PHP中檢查上傳的文件類型的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!