<?php
/*
* 判断文件是不是图片格式
* @param fileName 文件名
* @return array 如果code为1,是图片;否则不是图片
* @author lee complet@163.com
*/
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) {
$fileType = array('code' => 1, 'type' => 'gif');
} elseif ($typeCode == 13780) {
$fileType = array('code' => 1, 'type' => 'png');
} else {
$fileType = array('code' => 0, 'type' => '非图片格式');
}
return $fileType;
}