Download a file with PHP (download.php?file=path_to_file)

michaelsanford

Translator, Web Developer
I've looked through the php.net help guides on fpasstrhru(); (http://www.php.net/manual/en/function.fpassthru.php)

Still for some reason I can't get the file that is being downloaded (either DOC or PDF or RTF for the most part) to take the name of the original file.

For some reason, it always names the downloaded file download.php, but the content of the file is what I expect.

Is that because of my Apache configuration (I'm not allowing headers to be set properly by PHP)?
 
PS Since there is a possibility that I will need to parse PDF (binary) or RTF/DOC (text) how should I properly set the Content-Type header?

The file name still is not preserved when I use header("Content-Disposition: attachment; filename=$fileName");
 
What browser are you using?
I remember having this problem with some versions of Safari.
 
I can't use a browser redirect, because it would totally defet using PHP in the first place (to hide the location of the file).

I've had this problem with all my browsers, interstingly (OmniWeb, IE)
 
I've seen this a lot. I'm not sure if it's an OS X issue or Safari or PHP.

Maybe you could search for other free PHP download scripts then open them up and see if they have any code your missing.
 
I've had a look at a lot of examples on the PHP site, but I guess it's just one of those bugs that'll have to get worked out in time :p

Thanks guys!
 
Originally posted by michaelsanford
I've looked through the php.net help guides on fpasstrhru(); (http://www.php.net/manual/en/function.fpassthru.php)

Still for some reason I can't get the file that is being downloaded (either DOC or PDF or RTF for the most part) to take the name of the original file.

For some reason, it always names the downloaded file download.php, but the content of the file is what I expect.

Is that because of my Apache configuration (I'm not allowing headers to be set properly by PHP)?
This is a browser thing... Sometimes you can trick it by submitting the form to your php page with a fake name following.

... Like this: download.php/my_file.pdf

BTW, even the vBulletin software that runs this forum has this problem when a post has an attachment in a format that can not be handled natively by the browers. (PNG images for example.)
 
Back
Top