How can I check if a file is an mp3 file or image file, other than check each possible extension?
11 Answers
Native ways 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.
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;
}

- 312,688
- 75
- 539
- 559
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().

- 442,112
- 142
- 972
- 1,088
-
2are you saying that i should check if a file is an image or not using getimagesize something like : if(!getimagesize(path)){print 'this file is not an image!';} ? getimagesize(); returns false if the file is not an image? – kmunky Jan 05 '10 at 14:39
-
1+1 for this answer... the only way to be sure an image is an image is to use a library (in this case there are built in ones for images) that actually opens the file and parses it like you are suggesting. This way you prevent somebody renaming an .XLS to .GIF and uploading it. – TravisO Jan 05 '10 at 14:43
-
@kmunky: exactly. Check out the manual link to see what image types the function recognizes. – Pekka Jan 05 '10 at 16:15
-
i use this to check if a uploaded file is really a image: $tempFile = $_FILES['image']['tmp_name']; $image = getimagesize($tempFile); if ($image['mime']!=='image/jpeg'){ echo "error"; return; } – robertdd May 02 '10 at 18:05
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;
}

- 151,645
- 95
- 393
- 500
<?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.
-
1
-
His question specifically says check the file type besides just checking the file extension, which is all mime_content_type does, it's basically a built in array of extensions, it has no idea what the file really is. – TravisO Jan 05 '10 at 21:50
-
Depreciated? "Invest in gold", I said, but they told me I was in on the ground floor: put your money in PHP functions, they said. I'm sorry I listened. – Ben Apr 26 '12 at 06:54
-
1@Burntime, @TravisO, @Ben, Correction to the answer: `mime_content_type()` was never actually deprecated. It was just [a bug](https://bugs.php.net/bug.php?id=71367) in the docs. – Matthijs Wessels May 22 '17 at 12:32
-
@MatthijsWessels as I said before, this function is just a dictionary, it doesn't open the file and analyze it's contents, so it doesn't solve the problem. – TravisO May 24 '17 at 17:20
-
1@TravisO sure, I'm just referring to the remarks about it being deprecated. I've found this being mentioned in many places (on the web, but also in our code base) which confused me a lot until I found that it was apparently just a mistake in the docs. I'm doing my 2 cents to correct that. – Matthijs Wessels May 25 '17 at 21:07
getimageinfo is best to find images . Check if return type is false .

- 862
- 11
- 18
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

- 11,276
- 3
- 37
- 37
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;

- 1,619
- 12
- 20
This function checks if the file is an image based on extension and mime and returns true if it's a browser compatible image...
function checkImage($image) {
//checks if the file is a browser compatible image
$mimes = array('image/gif','image/jpeg','image/pjpeg','image/png');
//get mime type
$mime = getimagesize($image);
$mime = $mime['mime'];
$extensions = array('jpg','png','gif','jpeg');
$extension = strtolower( pathinfo( $image, PATHINFO_EXTENSION ) );
if ( in_array( $extension , $extensions ) AND in_array( $mime, $mimes ) ) return TRUE;
else return FALSE;
}

- 3,103
- 2
- 21
- 22
For Images, I use:
function is_image($path)
{
$a = getimagesize($path);
$image_type = $a[2];
if(in_array($image_type , array(IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP)))
{
return true;
}
return false;
}

- 1,863
- 24
- 8
The best way is to use finfo_file function. Example:
<?php
if (isset($_FILES['yourfilename']['tmp_name'])) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES['yourfilename']['tmp_name']);
if ($mime == 'image/jpg') {
echo "It's an jpg image!";
}
finfo_close($finfo);
}
?>

- 456
- 4
- 10
This function get a file path and with use finfo_open
and mime_content_type
if supported, return image
or video
or audio
string.
/**
* get file type
* @return image, video, audio
*/
public static function getFileType($file)
{
if (function_exists('finfo_open')) {
if ($info = finfo_open(defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME)) {
$mimeType = finfo_file($info, $file);
}
} elseif (function_exists('mime_content_type')) {
$mimeType = mime_content_type($file);
}
if (strstr($mimeType, 'image/')) {
return 'image';
} else if (strstr($mimeType, 'video/')) {
return 'video';
} else if (strstr($mimeType, 'audio/')) {
return 'audio';
} else {
return null;
}
}

- 9,887
- 6
- 59
- 81