File downloads - Content Disposition in different browsers..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RFairey
Forum Commoner
Posts: 52
Joined: Fri Jun 06, 2003 5:23 pm

File downloads - Content Disposition in different browsers..

Post by RFairey »

I have some files that I want a PHP script to send to the browser.

Using the following code:

Code: Select all

<?php

    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="$file"');
    readfile('music/'.$file);

?>
The URL for this has the format test.php?file=file_to_be_downloaded

In IE, I get a download box with the suggested filename equal to the whole URL:

You have chosen to download
test.php?file=file_to_be_downloaded
from servername

In Netscape, it gets the filename right but appends .php onto it:

You have chosen to download
file_to_be_downloaded.extension.php
from servername

In Opera and Mozilla however, it seems to work without a hitch. Are there any weird workarounds I should know about?

The files are held on a UNIX server running apache. They've all been chmodded so that only the script can read them, and the files can't be accessed directly.
slimsam1
Forum Commoner
Posts: 49
Joined: Wed Aug 21, 2002 12:20 am

Post by slimsam1 »

I'm thinking maybe IE takes everything after the last forward slash? I don't know that for a fact, but you might try using a query like download.php/filename.zip

You can use this, but make sure to make it more secure by hard-coding the downloads dir and filtering out all double-periods ".."


<?

header('Content-type: PUT THE TYPE HERE');
readfile(substr($_SERVER[PATH_INFO],1));

?>
slimsam1
Forum Commoner
Posts: 49
Joined: Wed Aug 21, 2002 12:20 am

Re: File downloads - Content Disposition in different browse

Post by slimsam1 »

RFairey wrote: <?php

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="$file"');
readfile('music/'.$file);

?>
Disregard my last post.... I believe the problem relates to your use of quotation marks. If memory serves me, strings in single quotes are not parsed for variables. It should be:

header("Content-Disposition: attachment; filename="$file"");

if the filename is supposed to be in quotes, that is.
RFairey
Forum Commoner
Posts: 52
Joined: Fri Jun 06, 2003 5:23 pm

Post by RFairey »

nope- doesn't work - last one changes nothing - the first post corrects the filename, but then IE complains that it could not downlaod the file.
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

I think it's probably the version of PHP you are using, this works on versions greater than 4.1:

Code: Select all

<?php 

    header('Content-Type: application/octet-stream'); 
    header("Content-Disposition: attachment; filename=".$_GET&#1111;'file']); 
    readfile("/usr/local/test/".$_GET&#1111;'file']); 

?>
Regards,
RFairey
Forum Commoner
Posts: 52
Joined: Fri Jun 06, 2003 5:23 pm

Post by RFairey »

Version 4.1.2
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

Did yolu try the revised code ?? Obviously replacing the readfile() path to reflect your configuration ?
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

I have had (and still have) similar troubles.
In IE, the download name is "&filename=file.ext";
In Mozilla, the download name is "file.ext.php".

I have never yet been able to make the header() download feature work correctly.

If I use a hard-coded directory and strip out the "..", IE will merely open the file if it can so any images, text files, etc are displayed in the browser, rather than saved to the local machine.
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

I've tried the code I posted on IE/Moz/Opera with PHP 4.2.2/4.3.0/4.3.2 and works fine for me, and I tried it with a GIF/JPEG/TXT file and all browsers asked me if I would like to download/save/open.

Could be an issue with your version of PHP.

Regards,
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Final Answer

Post by daven »

In order to avoid the .php appending from Netscape and the query-string behavior from IE, declare the relevant content-type for the file (ex: *.txt as text/plain, *.jpg as image/jpg, etc). To do so, use the unix file command as follows:

Code: Select all

<?php
$mimetype=exec("file -i -b $filename");

header("Content-disposition: attachment; filename=$filename");
header("Content-type: $mimetype");
header("Pragma: no-cache");
header("Expires: 0");
readfile($filename);
?>
Granted, this is kind of a hack, but it makes things work if they did not before.

Happy coding.
Post Reply