skip to Main Content

I’m trying to get the mimeType of audio files. For .amr file, all I can get is application/octet-stream.
I know it means apache server couldn’t guess the mimetype, because nothing seems to be related in magic.mime file. I tried every mimeType of php and symfony, but since it’s an issue with apache server, i don’t know what to do
Any advice ?

2

Answers


  1. Usually you can identify the file type with the first characters in the file, an arm file say #!AMR

    enter image description here

    or you can try something like this:

    echo getImageMimeType(file_get_contents ("yourFile.amr"));
    
    function getBytesFromHexString($hexdata)
    {
      for($count = 0; $count < strlen($hexdata); $count+=2)
        $bytes[] = chr(hexdec(substr($hexdata, $count, 2)));
    
      return implode($bytes);
    }
    
    function getImageMimeType($imagedata)
    {
      $imagemimetypes = array(
        "jpeg" => "FFD8", 
        "png" => "89504E470D0A1A0A", 
        "gif" => "474946",
        "bmp" => "424D", 
        "tiff" => "4949",
        "tiff" => "4D4D"
      );
    
      foreach ($imagemimetypes as $mime => $hexbytes)
      {
        $bytes = getBytesFromHexString($hexbytes);
        if (@substr($imagedata, 0, strlen($bytes)) == $bytes)
          return $mime;
      }
    
      /// This is to get the hexa char of amr
      echo $bytes;
    
      return NULL;
    }
    
    Login or Signup to reply.
  2. It sounds like you are trying to serve static files with Apache – in which case this question has got nothing to do with PHP. You just need to tell the webserver to return a mime type of audio/AMR for files with a .AMR extension.

    I don’t know what you’ve tried with PHP, but it’s not hard:

    <?php
    
    header("Content-type: audio/AMR");
    print file_get_contents("afile.amr");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search