Google Answers Logo
View Question
 
Q: Alternate to RightClick & Save Target As to download Mp3 file (PHP or JavaScrp) ( Answered,   2 Comments )
Question  
Subject: Alternate to RightClick & Save Target As to download Mp3 file (PHP or JavaScrp)
Category: Computers > Programming
Asked by: ask4work_474-ga
List Price: $12.00
Posted: 01 Mar 2003 06:04 PST
Expires: 31 Mar 2003 06:04 PST
Question ID: 169198
Using PHP or JAVASCRIPT, is there a way to save an Mp3 file to the
local drive without "Right Click" and then "Save Target As."  I would
like to have the client simply click on a link and have it begin
downloading the file.
Answer  
Subject: Re: Alternate to RightClick & Save Target As to download Mp3 file (PHP or JavaS
Answered By: nickyspag-ga on 02 Mar 2003 07:39 PST
 
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
Comments  
Subject: Re: Alternate to RightClick & Save Target As to download Mp3 file (PHP or JavaScrp)
From: j_philipp-ga on 02 Mar 2003 01:11 PST
 
Ask4work_474,

JavaScript or PHP cannot influence what happens when someone downloads
an MP3 file. There are various ways to initiate the download, but what
happens then -- wether some player starts automatically, or the user
will be prompted for saving -- depends on client-side browser and
system settings. All you can do is suggest to the user to change
settings and download certain software players.
Subject: Re: Alternate to RightClick & Save Target As to download Mp3 file (PHP or JavaScrp)
From: joey-ga on 02 Mar 2003 01:36 PST
 
That's not exactly correct.  To get it to play automatically (or to at
least provide the user with a dialogue box that will allow it to play
automatically), you need to adjust the mime type for MP3s on your
server.

If you're using Apache, you can do this in the .htaccess file located
in the directory that the MP3 is housed in.  If you are unable to
add/change a mime type, you can sort of cheat by using PERL or PHP to
serve the file.

To use the trick (which is less desirable than just changing the mime
type), you'd make the link to the PHP/PERL script instead of to the
MP3 file.  Then, you'd print to the browser a header that notes the
MP3 mime type.  I'm not exactly sure what it is off the top of my
head.  Then, you could read in the file and "print" it out to the
browser.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy