How can I check if a file is an mp3 file or image file, other than check each possible extension?
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.
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.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With