PHP 查看真实文件类型、图片有效性

思路:通过文件头确定文件类型,可使用Notepad++以16进制查看文件头。

方法一:

// 获取mime type
print_r(mime_content_type($path));

输出:image/svg+xml

方法二:

function test()
{
    $file_type_start = [
        "ffd8ffe000104a464946" => "jpg", //JPEG (jpg)
        "89504e470d0a1a0a0000" => "png", //PNG (png)
        "47494638396126026f01" => "gif", //GIF (gif)
        "49492a00227105008037" => "tif", //TIFF (tif)
        "424d228c010000000000" => "bmp", //16色位图(bmp)
        "424d8240090000000000" => "bmp", //24位位图(bmp)
        "424d8e1b030000000000" => "bmp", //256色位图(bmp)
        "41433130313500000000" => "dwg", //CAD (dwg)
        "3c21444f435459504520" => "html", //HTML (html)
        "3c21646f637479706520" => "htm", //HTM (htm)
        "48544d4c207b0d0a0942" => "css", //css
        "696b2e71623d696b2e71" => "js", //js
        "7b5c727466315c616e73" => "rtf", //Rich Text Format (rtf)
        "38425053000100000000" => "psd", //Photoshop (psd)
        "46726f6d3a203d3f6762" => "eml", //Email [Outlook Express 6] (eml)
        "d0cf11e0a1b11ae10000" => "doc", //MS Excel 注意:word、msi 和 excel的文件头一样
        "d0cf11e0a1b11ae10000" => "vsd", //Visio 绘图
        "5374616E64617264204A" => "mdb", //MS Access (mdb)
        "252150532D41646F6265" => "ps",
        "255044462d312e350d0a" => "pdf", //Adobe Acrobat (pdf)
        "2e524d46000000120001" => "rmvb", //rmvb/rm相同
        "464c5601050000000900" => "flv", //flv与f4v相同
        "00000020667479706d70" => "mp4",
        "49443303000000002176" => "mp3",
        "000001ba210001000180" => "mpg", //
        "3026b2758e66cf11a6d9" => "wmv", //wmv与asf相同
        "52494646e27807005741" => "wav", //Wave (wav)
        "52494646d07d60074156" => "avi",
        "4d546864000000060001" => "mid", //MIDI (mid)
        "504b0304140000000800" => "zip",
        "526172211a0700cf9073" => "rar",
        "235468697320636f6e66" => "ini",
        "504b03040a0000000000" => "jar",
        "4d5a9000030000000400" => "exe",//可执行文件
        "3c25402070616765206c" => "jsp",//jsp文件
        "4d616e69666573742d56" => "mf",//MF文件
        "3c3f786d6c2076657273" => "xml or svg",//xml文件
        "494e5345525420494e54" => "sql",//xml文件
        "7061636b616765207765" => "java",//java文件
        "406563686f206f66660d" => "bat",//bat文件
        "1f8b0800000000000000" => "gz",//gz文件
        "6c6f67346a2e726f6f74" => "properties",//bat文件
        "cafebabe0000002e0041" => "class",//bat文件
        "49545346030000006000" => "chm",//bat文件
        "04000000010000001300" => "mxp",//bat文件
        "504b0304140006000800" => "docx",//docx文件
        "d0cf11e0a1b11ae10000" => "wps",//WPS文字wps、表格et、演示dps都是一样的
        "6431303a637265617465" => "torrent",
        "d4c3b2a1020004000000" => "pcap", // 网络抓包文件

        "6D6F6F76" => "mov", //Quicktime (mov)
        "FF575043" => "wpd", //WordPerfect (wpd)
        "CFAD12FEC5FD746F" => "dbx", //Outlook Express (dbx)
        "2142444E" => "pst", //Outlook (pst)
        "AC9EBD8F" => "qdf", //Quicken (qdf)
        "E3828596" => "pwl", //Windows Password (pwl)
        "2E7261FD" => "ram", //Real Audio (ram)
    ];
    
    $file_path_list = [
        'D:\test_image\开源协议选择流程图-1 - 副本.txt',
        'D:\test_image\aaa.webp',
        'D:\test_image\1635832464(1).png',

        'D:\test_image\dump.pcap',
        'D:\test_image\aitest.svg',
    ];
    foreach ($file_path_list as $path) {
        $file = fopen($path, 'rw');
        $str = fread($file, 10);
        $str_to_hex= bin2hex($str);

        $file_type = '未知';
        $file_type_str = $str_to_hex;
        if (isset($file_type_start[$str_to_hex])) {
            $file_type = $file_type_start[$str_to_hex];
        } else {
            foreach ($file_type_start as $key => $val) {
                if (strpos(strtolower($str_to_hex), strtolower($key)) === 0) {
                    $file_type = $val;
                    $file_type_str = strtolower($key);
                    break;
                }
            }
        }
        echo "文件:" . $path . ':' . "</br>";
        echo "开头文本:" . $str . ' 长度:' . strlen($str) . "</br>";
        echo "开头文本hex:" . $str_to_hex . ' 长度:' . strlen($str_to_hex) . "</br>";
        echo '文件类型:' . $file_type . "(start:". $file_type_str . ")" . "</br>";
        if (@getimagesize($path)) {
            echo '是有效的图片' . "</br>";
        } else {
            echo '无效的图片' . '</br>';
        }
        echo "</br>";
        fclose($file);
    }
}

输出:

文件:D:\test_image\开源协议选择流程图-1 - 副本.txt:
开头文本:����JFIF 长度:10
开头文本hex:ffd8ffe000104a464946 长度:20
文件类型:jpg(start:ffd8ffe000104a464946)
是有效的图片

文件:D:\test_image\aaa.webp:
开头文本:����JFIF 长度:10
开头文本hex:ffd8ffe000104a464946 长度:20
文件类型:jpg(start:ffd8ffe000104a464946)
是有效的图片

文件:D:\test_image\1635832464(1).png:
开头文本:�PNG  长度:10
开头文本hex:89504e470d0a1a0a0000 长度:20
文件类型:png(start:89504e470d0a1a0a0000)
是有效的图片

文件:D:\test_image\dump.pcap:
开头文本:�ò� 长度:10
开头文本hex:d4c3b2a1020004000000 长度:20
文件类型:pcap(start:d4c3b2a1020004000000)
无效的图片

文件:D:\test_image\aitest.svg:
开头文本:开头文本hex:3c3f786d6c2076657273 长度:20
文件类型:xml or svg(start:3c3f786d6c2076657273)
无效的图片

16进制文件头(相关:JPG文件头结构介绍)
在这里插入图片描述

参考链接:
notepad怎么查看16进制编码
根据文件头数据判断文件类型
PHP图片损坏检测
JPG文件头结构介绍
理解JPEG文件头的格式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值