Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a file is mp3 or image file?

Tags:

How can I check if a file is an mp3 file or image file, other than check each possible extension?

like image 904
kmunky Avatar asked Jan 05 '10 14:01

kmunky


People also ask

Is MP3 image or audio?

Simply speaking, MP3 is a file extension for an audio file. OR to be a little more precise - it is a digital audio encoding format that can be used to effectively compress sound files with minimal loss in percieved quality.

Is MP3 an image file format?

An MP3 file is an audio file saved in a compressed audio format developed by the Moving Picture Experts Group (MPEG) that uses "Layer 3" audio compression (MP3). MP3 files are commonly used to store audio tracks, podcasts, lectures, sermons, and audiobooks.


1 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; } 
like image 146
Gordon Avatar answered Sep 20 '22 13:09

Gordon