How to get the file extension in PHP?

Possible Duplicate:
How to extract a file extension in 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?


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);

这也会起作用:

$array = explode('.', $_FILES['image']['name']);
$extension = end($array);

A better method is using strrpos + substr (faster than explode for that) :

$userfile_name = $_FILES['image']['name'];
$userfile_extn = substr($userfile_name, strrpos($userfile_name, '.')+1);

But, to check the type of a file, using mime_content_type is a better way : http://www.php.net/manual/en/function.mime-content-type.php

链接地址: http://www.djcxy.com/p/20172.html

上一篇: 在Markdown和reStructuredText中都有相同的README

下一篇: 如何在PHP中获取文件扩展名?