How to get the file type in PHP
I have a .doc
file and I renamed named it to give it a .jpg
extension. When I process the renamed 开发者_如何学运维file with my function it accepts the file as having a .jpg
although the file is not really a JPEG. What's the best way to find the actual file type? Here is my current code:
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
What's the best way to check the file's type without depending on the extension?
You can use PHP's Fileinfo to detect file types based on their contents; however, it is only available in PHP 5.3.0 or if you have access to install PECL extensions.
It returns a file's mime-type, such as text/html, image/gif, or application/vnd.ms-excel which should be more accurate (accounts for contents possibly having multiple common extensions, such as: jpg, jpe, jpeg).
This is the one I use and it has never failed me.
function getExtension($str)
{
$i = explode('.', $str);
return strtolower(end($i));
}
I don't know if you can check the actual original file extension if someone has renamed it, you would have to have the spec of every file type you were checking for I'm not sure how easy it is to tell. But you could verify something was an image like this:
function isImage($file)
{
if(@getimagesize($file) == false)
return false;
else
return true;
}
Summing up
if(isImage($file) && getExtention($file) == "jpg")
{
//Process, it's a valid image
}
Firstly, with respect to file extension, on't reinvent the wheel. Use PHP's pathinfo()
function:
$extension = pathinfo($filename, PATHINFO_EXTENSION);
Secondly, you're quite right: the file extension tells you nothing about it's contents.
Third, you should where possible determine the contents of the file yourself rather than relying on either the file extension or the MIME type (from file uploads). Both are arbitrary and simply specified by the client.
Images are fairly easy because you can load the file with the GD library. It'll fail if its not an image and you can interrogate it for size, etc.
Word documents are harder. If you're running on Windows, you can make a call to the system to load the file and see if it does load. I'm not aware of any native PHP library that can do this.
If you just want to get the file extension, you could use pathinfo
:
function getExtension($str)
{
$info = pathinfo($str)
return $info['extension'];
}
However, it sounds like you want to check if the file you're given is a valid image. To do that, you can do something similar with the getimagesize()
function:
function getMimeType($str)
{
$info = @getimagesize($str)
if (!$info)
{
// Uh oh, this wasn't a valid image -- do some error handling
return NULL;
}
return $info['mime'];
}
Using this, you can check the MIME type of the image ("image/jpeg", etc.), and validate if the given file is really the type of image you think it is.
You can use explode(".", $sFileName)
and take the last item of the array to do this, but that one fails when you have something like filename.tar.gz
, when you want the last two array items, or filename.class.php
, when, after you have implemented the last-two-array-items-thing, you just want the last one.
The best way of doing this is, in my opinion, pathinfo($sFileName)
. This returns an array with the keys dirname
, basename
, extension
(if any), and filename
.
If you want the file type, though, you'll have to use something like PHP 5.3's built-in Fileinfo to get the file's MIME-type. (getimagesize()
returns the MIME-type too, but this only works on images!)
function file_extension($filename) {
$path_info = pathinfo($filename);
return $path_info['extension'];
}
精彩评论