Home  >  Article  >  Backend Development  >  How to get the file type in php (without suffix)

How to get the file type in php (without suffix)

青灯夜游
青灯夜游Original
2022-01-10 18:18:205298browse

获取文件类型的方法:1、使用“$_FILES['uploadfile']['type']”语句;2、使用“finfo_file(finfo_open(FILEINFO_MIME),文件路径)”语句;3、通过读取文件头六个字节来进行判断。

How to get the file type in php (without suffix)

本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑

php获取文件类型(不通过后缀)

方法1:$_FILES

如果是php上传文件,则可以用$_FILES['uploadfile']['type']来获取文件类型,但是同样会存在和pathinfo同样的问题,该方法仅仅只能根据文件后缀来判断文件类型。

方法2:php Fileinfo 获取文件MIME类型(finfo_open)

代码示例:

<?php $finfo = finfo_open(FILEINFO_MIME); // 返回 mime 类型
    $filename = &#39;.\Uploads\2.zip&#39;;
    var_dump(finfo_file($finfo, $filename));
    finfo_close($finfo);
    die;

返回结果:

How to get the file type in php (without suffix)

说明: 如果文件存在则返回文件类型,否则返回false。该方法需要php5.3.0+版本。可以根据返回的结果来判断是什么类型的文件。该方法即便是原文件被改过后缀,已然可以读到原文件类型。

方法3:读取文件头六个字节作为判断

<?php
// 官方示例
function minimime($fname) {
    $fh=fopen($fname,&#39;rb&#39;);
    if ($fh) { 
        $bytes6=fread($fh,6);
        fclose($fh); 
        if ($bytes6===false) return false;
        if (substr($bytes6,0,3)=="\xff\xd8\xff") return &#39;image/jpeg&#39;;
        if ($bytes6=="\x89PNG\x0d\x0a") return &#39;image/png&#39;;
        if ($bytes6=="GIF87a" || $bytes6=="GIF89a") return &#39;image/gif&#39;;
        return &#39;application/octet-stream&#39;;
    }
    return false;
}
// 将文件头4个字节转换成16进制判断
function fileType($filename) {
    // 读取文件的前4个字节,根据硬编码判断
    $file = fopen ( $filename, "rb" );
    $strFile = fread ( $file, 4 ); //只读文件头4字节
    fclose ( $file );
    $strInfo = @unpack ( "C4chars", $strFile );  
    //dechex(),把十进制转换为十六进制。  
    $code = dechex ( $strInfo [&#39;chars1&#39;] ) .   
            dechex ( $strInfo [&#39;chars2&#39;] ) .   
            dechex ( $strInfo [&#39;chars3&#39;] ) .   
		    dechex ( $strInfo [&#39;chars4&#39;] );  
    $type = &#39;&#39;;  
    switch ($code) //硬编码值查表  
    {
        case "504b34" :  
            $type = &#39;application/zip; charset=binary&#39;;  
            break;
        case "89504e47" :
            $type = &#39;image/png; charset=binary&#39;;  
            break; 
        default :
            $type = false;  
            break;
    }
    return $type;
}

说明: 这个方法有缺陷,不同类型的文件,文件头4个字节可能会相同,并且部分文件类型表示文件类型的字符串,少于4个字节。可以考虑将方法2和方法3结合使用。

文件类型硬编码值的对照表:http://www.garykessler.net/library/file_sigs.html
(对照表的英文大写要改成小写,第一个数字为0时要省略,eg:504B0304读到的是504b34)

扩展知识:通过后缀获取文件类型

pathinfo方法

代码示例:

/** 
* 获取文件后缀(如果文件名为11.11,11不是后缀,会默认11为后缀)
* $file string 文件路径或者文件名
*/
function get_extension($file){
return pathinfo($file, PATHINFO_EXTENSION);

说明: pathinfo具体使用方法,可以查看php手册。但是该方法仅仅只能根据文件后缀来判断文件类型,如果html后缀的文件,被修改成.php的后缀之后,读取到的则是php类型文件。

推荐学习:《PHP视频教程

The above is the detailed content of How to get the file type in php (without suffix). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn