今天就跟大家聊聊有关php中怎么获取图片真实后缀,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
一、getimagesize(支持base64):
代码:
<?php function getimgsuffix($name) { $info = getimagesize($name); $suffix = false; if($mime = $info['mime']){ $suffix = explode('/',$mime)[1]; } return $suffix; }
测试:
$suffix = getimgsuffix('/file/upload/202210/19/34m255h1oow.jpg'); echo $suffix;
输出:
jpeg
二、二进制方法:
代码:
<?php function getfilesuffix($name) { $file = fopen($name, "rb"); $bin = fread($file, 2); // 只读2字节 fclose($file); $info = @unpack("C2chars", $bin); $code = intval($info['chars1'] . $info['chars2']); $suffix = "unknow"; if($code == 255216){ $suffix = "jpg"; }elseif($code == 7173){ $suffix = "gif"; }elseif($code == 13780){ $suffix = "png"; }elseif($code == 6677){ $suffix = "bmp"; }elseif($code == 7798){ $suffix = "exe"; }elseif($code == 7784){ $suffix = "midi"; }elseif($code == 8297){ $suffix = "rar"; }elseif($code == 7368){ $suffix = "mp3"; }elseif($code == 0){ $suffix = "mp4"; }elseif($code == 8273){ $suffix = "wav"; } return $suffix; }
测试:
$suffix = getfilesuffix('/file/upload/202210/19/34m255h1oow.jpg'); echo $suffix;
输出:
jpg
看完上述内容,你们对php中怎么获取图片真实后缀有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网行业资讯频道,感谢大家的支持。