Skip to content

Instantly share code, notes, and snippets.

@AlexPashley
Created August 1, 2013 11:20
Show Gist options
  • Save AlexPashley/6130489 to your computer and use it in GitHub Desktop.
Save AlexPashley/6130489 to your computer and use it in GitHub Desktop.
PHP: function to check if a file is an audio file, returns boolean response Requires PHP >= 5.3
/**
* Gets real MIME type and then see if its on allowed list
*
* @param string $tmp : path to file
*/
function check_file_is_audio( $tmp )
{
$allowed = array(
'audio/mpeg', 'audio/x-mpeg', 'audio/mpeg3', 'audio/x-mpeg-3', 'audio/aiff',
'audio/mid', 'audio/x-aiff', 'audio/x-mpequrl','audio/midi', 'audio/x-mid',
'audio/x-midi','audio/wav','audio/x-wav','audio/xm','audio/x-aac','audio/basic',
'audio/flac','audio/mp4','audio/x-matroska','audio/ogg','audio/s3m','audio/x-ms-wax',
'audio/xm'
);
// check REAL MIME type
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$type = finfo_file($finfo, $tmp );
finfo_close($finfo);
// check to see if REAL MIME type is inside $allowed array
if( in_array($type, $allowed) ) {
return true;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment