Page 1 of 1
File downloads - Content Disposition in different browsers..
Posted: Tue Jun 10, 2003 4:53 pm
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.
Posted: Tue Jun 10, 2003 6:56 pm
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));
?>
Re: File downloads - Content Disposition in different browse
Posted: Tue Jun 10, 2003 8:04 pm
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.
Posted: Wed Jun 11, 2003 2:47 pm
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.
Posted: Wed Jun 11, 2003 3:52 pm
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ї'file']);
readfile("/usr/local/test/".$_GETї'file']);
?>
Regards,
Posted: Thu Jun 12, 2003 6:36 am
by RFairey
Version 4.1.2
Posted: Thu Jun 12, 2003 7:30 am
by cactus
Did yolu try the revised code ?? Obviously replacing the readfile() path to reflect your configuration ?
Posted: Thu Jun 12, 2003 1:20 pm
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.
Posted: Thu Jun 12, 2003 1:38 pm
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,
Final Answer
Posted: Mon Jun 16, 2003 1:08 pm
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.