Hi guys and girls,
I'm having some problems with forcing a file download.
I want to use the script to force the download of .doc,.pdf and .txt files.
I did some searching on these forums and it already helped me alot!
I already have a script that works on most browsers (ie5.01, netsc4.07, netsc6.01, opera4.01) except for IE 5.5 and Opera 5.
IE 5.5 just opens the file in the browserwindow, Opera 5 just opens raw code in the browserwindow (as if they were textfiles). I already had to use a little hack to please IE 5.5, otherwise download.php got downloaded instead of the actual file...
Please have a look at the code below.
All tips are appreciated!
Thanx alot!
Bert
This is what I've got for now:
<?
//download.php, a result of some serious cutting and pasting...
//download.php?file.ext will get 'file.ext' from the download directory
//download.php?somesubdir/file.ext will get 'file.ext' from 'somesubdir' in the download directory
$what_to_get = getenv("QUERY_STRING");
//puts cookie,
SetCookie("Download",yep, time()+36000000, "/", "www.domain.com", 0);
//headerstuff
$size = filesize("/path/to/the/download/dir/$what_to_get");
//we wanna know the name of the file, without the directories
$my_name_is = basename($what_to_get);
//use one of these 2, I don't know if it makes a difference...
//header("Content-Type: application/octet-stream");
header("Content-Type: application/force-download");
//need the size of our file
header("Content-Length: $size");
// IE5.5 just downloads download.php if we don't do this...
// for now I only check on 5.5 but what if 6.0 has the same problem??
if(preg_match("/MSIE 5.5/", $HTTP_USER_AGENT)) {
header("Content-Disposition: filename=$my_name_is");
}
else {
header("Content-Disposition: attachment; filename=$my_name_is");
}
header("Content-Transfer-Encoding: binary");
$fh = fopen("/path/to/the/download/dir/$what_to_get", "r");
fpassthru($fh);
exit;
?>