In most of my applications where download is required, I sometimes agree to check the MIME, which is sent by the browser (client), to a list of predefined MIME types. This approach makes the general assumption that if something suspicious occurs when the browser cannot transfer the MIME type of the downloaded file, I probably do not want to process it at this time.
<?php $valid_mp3_mimes = array( 'audio/mpeg', 'audio/x-mpeg', 'audio/mp3', 'audio/x-mp3', 'audio/mpeg3', 'audio/x-mpeg3', 'audio/x-mpeg-3', 'audio/mpg', 'audio/x-mpg', 'audio/x-mpegaudio', 'video/mpeg', 'video/x-mpeg', ); $uploaded_file_mime = $_FILES['upload_field_name']['type']; if(!in_array($uploaded_file_mime, $valid_mp3_mimes)) { die('Upload is not a valid MP3 file.'); }
You may or may not feel that this is a sufficient method for your purposes. The PHP manual explicitly states that this information is available if the browser provided this information and that the MIME type is NOT marked on the server side and therefore should not be taken for granted.
One thing to consider is the availability of resources on the server, which allow you to authenticate the true MIME file type.
As PHP developers, we like the great flexibility of creating platform-independent code (for example, our web applications built on Windows with XAMPP can be deployed on a Linux hosting environment with very little change). However, when checking for MIME types, we begin to introduce platform-specific methods that require checking for the existence of these tools (such as "file" or "finfo_file").
This may be one implementation worth exploring (taken from the CodeIgniter GitHub repository) that uses these tools and describes the working example about how you intend to get within PHP:
File The MIME type detects the (actual) MIME type of the downloaded file, if possible. https://github.com/EllisLab/CodeIgniter/blob/develop/system/libraries/Upload.php#L983
Sources
PHP Guide Downloading the POST Method - http://www.php.net/manual/en/features.file-upload.post-method.php
Webmaster Toolkit Mime Types - http://www.webmaster-toolkit.com/mime-types.shtml
FILExt .MP3 File - http://filext.com/file-extension/MP3