I am building a website where the user can upload mp3 files. And, I want to check if the file is a mp3 file. What I need to know is if there is a php function that do that for me.
I tried $_FILES['uploadedfile']['type']
But, I does not work like I want, because it returns the file type based on the extension (I tested it only on windows)
So, the questions are:
- There is a safe way to do that with php native functions??
- The file type based on extension, is a php issue or is related to the OS?
- What do you recomend to me??
Thanks.
For MP3s, not with PHP native functions, no.
There is
fileinfothat makes use of the OS’s “MIME sniffing” functionality. If installed, it’s usually pretty reliable.Alternatively, the getID3 library claims to be able to extract information from MP3 files. If you can get a playing time from the file, chances are it is a valid MP3 file.
Neither. The browser determines the file type (according to extension usually) and sends a MIME type. As with any incoming data, it is very easy to fake and can never be trusted.