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
getimageinfo is best to find images . Check if return type is false .
You could use finfo like this:
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):
Then just pass the mime file type to a switch statement like:
You can identify image files using
getimagesize
.To find out more about MP3 and other audio/video files, I have been recommended
php-mp4infogetID3().This function checks if the file is an image based on extension and mime and returns true if it's a browser compatible image...
The best way is to use finfo_file function. Example:
This function get a file path and with use
finfo_open
andmime_content_type
if supported, returnimage
orvideo
oraudio
string.