Hi
Firstly, I think meddling with MIME types is a bit naughty, because it
should be up to the user to determine how they want different types of
data handled, however I realise in practice that Internet Explorer and
other browsers don't make it very easy for average users to control
these things. So here is how to 'force' a "Save As" dialogue in PHP,
as per joey-ga's second suggestion.
Take a look at the following script:
<?
if ($_GET["getmp3"]) {
header("Content-Type: application/x-octet-stream");
header("Content-Disposition: attachment;
filename=suggestedfilename.mp3");
readfile("mymp3.mp3");
exit();
}
?>
<html>
<head>
<title>Mp3 MIME Type test</title>
<body>
<p><a href="mymp3.mp3">1. Download mymp3.mp3</a></p>
<p><a href="index.php?getmp3=1">2. Download mymp3.mp3</a></p>
</body>
</html>
<? //Ends ?>
Assume that the above script is named "index.php", and the mp3 you
want to serve is called "mymp3.mp3"
There are two download links in the above script. The first links
directly to the mp3 file. The second links to the PHP script, with a
variable/value in the querystring ("getmp3=1").
At the top of the script, if that variable is true, the script sets
two HTTP headers and outputs the mp3 file, using readfile(), and then
exits to avoid the HTML at the bottom being grafted onto the end.
The first header sets the Content-Type to
"application/x-octet-stream". This MIME type is a generic binary type,
as opposed to the specific audio-mpeg or x-mp3 types.
The second header tells the browser to treat the data as a file to be
saved to disk. You can suggest a default filename for the popup
dialogue box ("filename=$filename") so that it doesn't contain the PHP
file extension, which may be confusing to average users.
Lastly, note that a potential problem with using readfile() is that
PHP will put the entire file in memory, so if your mp3 is larger than
8MB (the default memory limit on a PHP script), you will need to use
fread() instead, or increase the memory limit in php.ini
Additional Links:
What is an an "application/octet-stream" MIME attachment and how can I
see it?
http://kb.indiana.edu/data/agtj.html
PHP Manual: readfile() function
http://www.php.net/manual/en/function.readfile.php
PHP Manual: fread() function
http://www.php.net/manual/en/function.fread.php
PHP Manual: header() function
http://www.php.net/manual/en/function.header.php
Search Strategy:
://www.google.com/search?q=MIME+application%2Foctet-stream |