天天看點

php判斷上傳檔案是不是圖檔,如果是,傳回圖檔格式

/

判斷檔案是不是圖檔格式

@param fileName 檔案名

@return array 如果code為1,是圖檔;否則不是圖檔

@author lee [email protected]

function isImg($fileName){

$file = fopen($fileName, "rb");

$bin = fread($file, 2); // 隻讀2位元組

fclose($file);

$strInfo = @unpack("C2chars", $bin);

$typeCode = intval($strInfo['chars1'].$strInfo['chars2']);

$fileType = array();

if($typeCode == 255216){

$fileType = array(

'code'=>1,

'type'=>'jpg'

);

}elseif($typeCode == 7173){

'type'=>'gif'

}elseif($typeCode == 13780){

'type'=>'png'

}else{

'code'=>0,

'type'=>'非圖檔格式'

}

return $fileType;

 本文轉自 Lee_吉 51CTO部落格,原文連結:http://blog.51cto.com/12173069/2059658