Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if the file is audio file in PHP

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!

like image 476
Muaaz Khalid Avatar asked Mar 22 '14 06:03

Muaaz Khalid


2 Answers

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.

like image 124
Muaaz Khalid Avatar answered Nov 10 '22 12:11

Muaaz Khalid


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';
        }
    }
}
?>
like image 1
Arryangga Aliev Pratamaputra Avatar answered Nov 10 '22 11:11

Arryangga Aliev Pratamaputra