how can i check if a file is an mp3 file or image file, other than check each possible extension?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
To find the mime type of a file I use the following wrapper function:
For Images, I use:
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
try mime_content_type()
Output:
Or better use finfo_file() the other way is deprecated.
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.