skip to Main Content

I am allowing users to upload files to my server, so I need to detect if a file is an audio file. I attempted to check based on the MIME type using mime_content_type($path) but often times it returns application/octet-stream. If it were an image I could use getimagesize() so is there an equivalent for audio files?

2

Answers


  1. what if you open the file directly to get the mime type

    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;
        }
    }
    
    Login or Signup to reply.
  2. Mp3 data detection is a wierd thing. There is a serious tradeof to be made between reading/processing alot of data or just looking for certain values at specified locations.

    As far as mime-type detection goes i think [correct me if i am wrong] application/octet-stream is the default fallback type.

    if you want a quick and dirty but performant solution i sugest just using the file extension *.mp3 to determine it’s type.

    If the reliabilty of this functionality is crucial [higher priority than performance]
    you can use a library like ffprobe [from ffmpeg].

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search