I'm writing code to upload audio file (could be in any format .mp3, mp4, .wav and many more...) I dont want to write all the conditions for all the mime types and then check uploaded file to validate the mime type. Because, I want to ACCEPT ALL the audio files(not just one or two formats).
So, is there any simple way to check whether the file is audio or not?
Thank you!
All the audio files format has "audio/"
common in MIME Type. So, we can check the $_FILES['file']['mime_type']
and apply a preg_match()
to check if "audio/"
exists in this mime type or not.
Here is the simple function
<?php
if(!function_exists('mime_content_type')) {
function mime_content_type($filename) {
$mime_types = array(
// audio/video
'mp3' => 'audio/mpeg',
);
$ext = strtolower(array_pop(explode('.',$filename)));
if (array_key_exists($ext, $mime_types)) {
return "THIS IS AUDIO FILES";
}
else {
return 'application/octet-stream';
}
}
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With