I have both .mp3 and .pcm audio format on my server, how should I set the php header content-type when I don't know which type the user has requested for?
-
4How *could* you not know which type the user has requested for? – Alvin Wong Nov 15 '12 at 09:04
-
i CAN know, but in my system, the user's request doesnt contain the audio format info, i need to retrieve that info from db, so i m seeking if there is a simple way. – Blake Nov 15 '12 at 09:08
4 Answers
Yes you have to .. you can use mime_content_type
to Detect MIME Content-type for a file simplify the process in your header
Example
header(sprintf('Content-type: %s',mime_content_type($filename)));

- 94,024
- 28
- 166
- 217
-
Why not use simple string concatenation? `header('Content-type: .mime_content_type($filename));` – Alvin Wong Nov 15 '12 at 09:11
I guess you can just use "Application/octet-stream" for any type of binary file format.

- 1,925
- 15
- 17
When you say you set the header the i think your rewrite your .mp3 and .pcm to a PHP-Script.
In this case you should rewrite it with the file extension. Then you can check the extension in your $_GET variable.
if(strpos('.mp3', $_GET['filename']) !== false) {
header('Content-Disposition: inline;filename="test.mp3"');
}
if(strpos('.pcm', $_GET['filename']) !== false) {
header("Content-type: application/octet-stream;");
}
PCM data is RAW-Data so i think you can use octet-stream or the cool solution from Baba
So you can check which format it is and set the header for the specified file.
Here is another post to this problem: Output mp3 with php

- 1
- 1

- 26,716
- 22
- 73
- 82
In my site i used this type of header. I suggests this one.
Example
header("content-type:text/xml;charset=utf-8;charset=windows-1256");

- 92
- 11