如何在PHP中获取文件扩展名?
问题描述:
我希望获取要上传的图像的文件扩展名,但我只获得了一个数组.
I wish to get the file extension of an image I am uploading, but I just get an array back.
$userfile_name = $_FILES['image']['name'];
$userfile_extn = explode(".", strtolower($_FILES['image']['name']));
有没有办法直接获得扩展名?
Is there a way to just get the extension itself?
答
无需使用字符串函数.您可以使用实际为您想要的东西而设计的东西: pathinfo()
:
No need to use string functions. You can use something that's actually designed for what you want: pathinfo()
:
$path = $_FILES['image']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);