how can i check if a file is an mp3 file or image file, other than check each possible extension?
You can identify image files using getimagesize
.
To find out more about MP3 and other audio/video files, I have been recommended php-mp4info getID3().
<?php
echo mime_content_type('php.gif') . "\n";
echo mime_content_type('test.php');
?>
Output:
image/gif
text/plain
Or better use finfo_file() the other way is deprecated.
Native way to get the mimetype:
For PHP < 5.3 use mime_content_type()
For PHP > 5.3 use finfo_fopen()
Alternatives to get the MimeType are exif_imagetype and getimagesize, but these rely on having the appropriate libs installed. In addition, they will likely just return image mimetypes, instead of the whole list given in magic.mime.
While mime_content_type
is removed from PHP5.3, it works fine below that version. E_STRICT
won't even raise a notice about it being deprecated. If you don't want to bother about what is available on your system, just wrap all four functions into a proxy method that delegates the function call to whatever is available, e.g.
function getMimeType($filename)
{
$mimetype = false;
if(function_exists('finfo_fopen')) {
// open with FileInfo
} elseif(function_exists('getimagesize')) {
// open with GD
} elseif(function_exists('exif_imagetype')) {
// open with EXIF
} elseif(function_exists('mime_content_type')) {
$mimetype = mime_content_type($filename);
}
return $mimetype;
}
You can use FileInfo module which is built into PHP since 5.3. If you are using a PHP version less than PHP 5.3, you can install it as a PECL extension:
After installation the finfo_file
function will return file information.
PECL extension: http://pecl.php.net/package/fileinfo
PHP Documentation: http://www.php.net/manual/en/book.fileinfo.php
You could use finfo like this:
$mime = finfo_open(FILEINFO_MIME, $path_to_mime_magic_file);
if ($mime ===FALSE) {
throw new Exception ('Finfo could not be run');
}
$filetype = finfo_file($mime, $filename);
finfo_close($mime);
or if you have problems with finfo not being installed, or the mime magic file just not working (it works correctly on 3 out of our 4 servers - all identical OS and PHP installs) - then try using Linux's native file (don't forget to sanitise the filename though: in this example, I know the filename can be trusted as it's a PHP temporary filename in my test code):
ob_start();
system('file -i -b '.$filename);
$output = ob_get_clean();
$output = explode("; ", $output);
if (is_array($output)) {
$filetype = trim($output[0]);
}
Then just pass the mime file type to a switch statement like:
switch (strtolower($filetype)) {
case 'image/gif':
return '.gif';
break;
case 'image/png':
return '.png';
break;
case 'image/jpeg':
return '.jpg';
break;
case 'audio/mpeg':
return '.mp3';
break;
}
return null;
To find the mime type of a file I use the following wrapper function:
function Mime($path)
{
$result = false;
if (is_file($path) === true)
{
if (function_exists('finfo_open') === true)
{
$finfo = finfo_open(FILEINFO_MIME_TYPE);
if (is_resource($finfo) === true)
{
$result = finfo_file($finfo, $path);
}
finfo_close($finfo);
}
else if (function_exists('mime_content_type') === true)
{
$result = preg_replace('~^(.+);.*$~', '$1', mime_content_type($path));
}
else if (function_exists('exif_imagetype') === true)
{
$result = image_type_to_mime_type(exif_imagetype($path));
}
}
return $result;
}