获取图片扩展名

OTA*_*TAR 13 php image

我想上传图片扩展名.

据我所知,最好的方法是getimagesize()功能.

但是这个函数的mime,image/jpeg在图像有.jpg或者也有.JPEG扩展时返回.

如何获得准确的扩展?

小智 26

$ext = pathinfo($filename, PATHINFO_EXTENSION);
Run Code Online (Sandbox Code Playgroud)


lup*_*tus 25

您可以使用image_type_to_extension返回的图像类型的函数getimagesize:

$info = getimagesize($path);
$extension = image_type_to_extension($info[2]);
Run Code Online (Sandbox Code Playgroud)


小智 6

您还可以使用strrpos和substr函数来获取任何文件的扩展名

$filePath="images/ajax-loader.gif";

$type=substr($filePath,strrpos($filePath,'.')+1);

echo "file type=".$type;
Run Code Online (Sandbox Code Playgroud)

输出:gif

如果你想扩展像.gif

$type=substr($filePath,strrpos($filePath,'.')+0);
Run Code Online (Sandbox Code Playgroud)

输出:.gif