tags:

views:

193

answers:

2

I'm trying to download a file through my server to me, by streaming the download.

This is my script:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=http://user:[email protected]/file.bin';
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $r[2]);
ob_clean();
flush();
readfile($fileName);
exit;

example.com isn't what I'm really using, I just didn't want to show the real URL. :) When I go to this file in my web browser a download prompt does not come up and nothing happens. $r[2] is the content length in bytes of the file and $fileName is the same URL as used in the attachment header. I really don't know what I'm doing wrong.

A: 

readfile(); need the absolute path of the file on the machine rather than a url.

e.g. /home/person/file.exe

Tom
Hi, I have tried this already and the script does still not work when I test it with a local file. Even if this did work, I still have the problem of opening a URL with readfile() don't I? php.net says this function accepts a URL but I can't get it to work. Thanks for the reply.
Joseph
From the php function page "A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the List of Supported Protocols/Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide."
Tim Lytle
A: 

There's a missing ) at the end of the filename header line, but I'm assuming that happened when edited the line.

For what it's worth, running your sample code on gives a download prompt, haven't really tested it much past that, but maybe it's something with the server/client configuration - not the code.

Tim Lytle