Page 1 of 1

Forcing download of a file

Posted: Tue Feb 19, 2008 10:51 am
by impulse()
I have a link on my website to an MP3 file using a standard <a href> tag. But this will redirect the user to the file, and if Apple Quicktime is installed Quicktime thinks it has the right to open the file in the browser and not allow for the option to download. I understand 'Save-As' is on option, but it's not in my case as I redirect to a getFile.php?id=<file ID in database> - so it'll just save the HTML output.

I have tried to force the following headers:

Code: Select all

header("Content-type: application/octet-stream");
header("Content-Disposition: inline; filename={$url}");
header("Content-length: ".(string)(filesize($url)));
header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
But this doesn't work as I don't have the size of the file for the 'Content-length' header. Could somebody suggest how I get the filesize of the MP3, which is on a different server or another way to go about this.

Thanks in advance,

Re: Forcing download of a file

Posted: Tue Feb 19, 2008 11:34 am
by choppsta
Try this header to force a download dialogue:

Code: Select all

header('Content-Disposition: attachment; filename='.$fileName.';');

Re: Forcing download of a file

Posted: Wed Feb 20, 2008 2:57 am
by impulse()
I tried what you've suggested but the files have a filesize of 0 bytes once downloaded and the files download instantly so it looks like it's not sending any data.

Re: Forcing download of a file

Posted: Wed Feb 20, 2008 3:18 am
by impulse()
Please remember that the file to download is on a different server to the one hosting the link to it, therefor I can only provide the URL of the file and can't give it a filesystem path.

Re: Forcing download of a file

Posted: Wed Feb 20, 2008 3:35 am
by choppsta
Not providing the file size shouldn't be an issue, it sounds like there's a problem with streaming the actual file.
How are you doing this, with readfile() or something?

Re: Forcing download of a file

Posted: Wed Feb 20, 2008 3:41 am
by impulse()
No, I'm simply creating a link directly to the file on a different server. The PHP script has nothing to do with reading the file I'm trying to download. The only outcome I want from the PHP script is to FORCE download of an MP3 file from another server.

EG:

Code: Select all

 
$query = mysql_query("SELECT url FROM mixes WHERE id = {$_GET['id']}");
$url   = mysql_result($query, 0, 0);
 
header("Location: $url");
 
But when directed direct to that file, if the PC has browser MP3 support then the program that plays MP3s handles it. Rather than using a 'Location' header I tried your 'Content-Disposition' header but it downloads 0 bytes. The file does exist as I can download or stream the file.

Re: Forcing download of a file

Posted: Wed Feb 20, 2008 4:01 am
by choppsta
By sending a Location header (i.e. a redirection 3xx response) you are passing the request off to the external server, which will then issue it's own headers. The headers you are setting will have no affect.

In order to control this you would need to stream the file yourself so that it comes from your server. Instead of sending a location header you could just do:

Code: Select all

readfile($url);