我想根据php读取的文件数据流来判断文件的mime-type,怎么实现呢,有高手吗?比如:$stream = file_get_contents($fileName);
如何根据$stream的内容判断mime-type呢.
不要用mime_content_type方法, 因为这个方法好像在php5.3下不怎么好用.
也不要用根据文件名辍返回的方法,因为这种方法不怎么可靠.
如何根据$stream的内容判断mime-type呢.
不要用mime_content_type方法, 因为这个方法好像在php5.3下不怎么好用.
也不要用根据文件名辍返回的方法,因为这种方法不怎么可靠.
http://topic.csdn.net/u/20110301/11/8e2153e2-5903-47a5-ae55-5612cf6084c1.html
这里有你说的。
curl
header("content-type:text/html;charset=gbk");
$mime = array(
'gif' => 'image/gif',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'bmp' => 'image/bmp',
'png' => 'image/png',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'pict' => 'image/x-pict',
'pic' => 'image/x-pict',
'pct' => 'image/x-pict',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'psd' => 'image/x-photoshop', 'swf' => 'application/x-shockwave-flash',
'js' => 'application/x-javascrīpt',
'pdf' => 'application/pdf',
'ps' => 'application/postscrīpt',
'eps' => 'application/postscrīpt',
'ai' => 'application/postscrīpt',
'wmf' => 'application/x-msmetafile', 'css' => 'text/css',
'htm' => 'text/html',
'html' => 'text/html',
'txt' => 'text/plain',
'xml' => 'text/xml',
'wml' => 'text/wml',
'wbmp' => 'image/vnd.wap.wbmp', 'mid' => 'audio/midi',
'wav' => 'audio/wav',
'mp3' => 'audio/mpeg',
'mp2' => 'audio/mpeg', 'avi' => 'video/x-msvideo',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime', 'lha' => 'application/x-lha',
'lzh' => 'application/x-lha',
'z' => 'application/x-compress',
'gtar' => 'application/x-gtar',
'gz' => 'application/x-gzip',
'gzip' => 'application/x-gzip',
'tgz' => 'application/x-gzip',
'tar' => 'application/x-tar',
'bz2' => 'application/bzip2',
'zip' => 'application/zip',
'arj' => 'application/x-arj',
'rar' => 'application/x-rar-compressed', 'hqx' => 'application/mac-binhex40',
'sit' => 'application/x-stuffit',
'bin' => 'application/x-macbinary', 'uu' => 'text/x-uuencode',
'uue' => 'text/x-uuencode', 'latex'=> 'application/x-latex',
'ltx' => 'application/x-latex',
'tcl' => 'application/x-tcl', 'pgp' => 'application/pgp',
'asc' => 'application/pgp',
'exe' => 'application/x-msdownload',
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'mdb' => 'application/x-msaccess',
'wri' => 'application/x-mswrite',
);
$url = 'http://tp4.sinaimg.cn/1692980395/180/1298526784/0';
//$url = 'http://www.baidu.com/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
curl_close($ch);if(preg_match('/Content-Type:\s+(.*?)(?:;|\s)/i',$contents,$arr)){
$mimestr = strtolower(trim($arr[1]));
$outkey = array_keys($mime,$mimestr);
echo '文件类型是:'.implode(',',$outkey) ;
}else{
echo '没有匹配到类型';
}
$f='cookie.txt';
$path_parts = pathinfo($f);
echo $path_parts['extension'], "\n"; //这个是扩展名
========================================
比如,我把gif文件的后辍改为".rar"之后得到的结果就是"Content-Type: application/x-rar-compressed".
这个除了getimagesize()能读出图片后缀改.rar的,其他的都读不出来。
========================================
客户端的,只能用js实现。
$bin = fread($file, 2); //只读2字节
fclose($file);
$strInfo = @unpack("c2chars", $bin);
$typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
$fileType = '';
switch ($typeCode)
{
case 7790:
$fileType = 'exe';
break;
case 7784:
$fileType = 'midi';
break;
case 8297:
$fileType = 'rar';
break;
case 255216:
$fileType = 'jpg';
break;
case 7173:
$fileType = 'gif';
break;
case 6677:
$fileType = 'bmp';
break;
case 13780:
$fileType = 'png';
break;
default:
echo 'unknown';
}
echo 'this is a(an) '.$fileType.' file:'.$typeCode;
是说的这个吧,去文件头信息,得到的是最标准的文件格式,改扩展名也是白搭的