Click to See Complete Forum and Search --> : How to force Download in IE...


la_unt
03-14-2004, 10:10 PM
Hi,

Would somebody could tell me how to force download in IE? I saw someone did a code (as shown below) and I 've tested it works in Netscape but does not work in IE, so I added some changes on it, but still can;t work in IE! Could sone of you kindly to show me why and how to fix it!

Thanks a lot!

=========
<?PHP
....
if (strstr($HTTP_USER_AGENT,"MSIE")) {
$attachment = " ";
} else {
$attachment = " attachment;";
}
$saveasname="extract.xls";
//header("Content-Type: application/octet-stream");
header('Content-Type: application/vnd.ms-excel');
//header("Content-Disposition: attachment; filename=extraction.xls");
//header('Content-Disposition: $attachment filename="extraction.xls";');
header('Content-Disposition: $attachment filename="'.$saveasname.'"');
header('Content-Transfer-Encoding: binary');
header('Pragma: no-cache');
header('Expires: 0');
print "$header\n$data";
?>

whiteadi
03-15-2004, 05:41 AM
<?php
// downloading a file
$filename = $HTTP_GET_VARS['path'];

// fix for IE catching or PHP bug issue
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// browser must download file from server instead of cache

// force download dialog
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");

// use the Content-Disposition header to supply a recommended filename and
// force the browser to display the save dialog.
header("Content-Disposition: attachment; filename=".basename($filename).";");

/*
The Content-transfer-encoding header should be binary, since the file will be read
directly from the disk and the raw bytes passed to the downloading computer.
The Content-length header is useful to set for downloads. The browser will be able to
show a progress meter as a file downloads. The content-lenght can be determines by
filesize function returns the size of a file.
*/
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));

readfile("$filename");
exit();

?>

pyro
03-15-2004, 07:09 AM
http://www.infinitypages.com/research/download.htm might help, as well.

chris9902
03-15-2004, 08:22 AM
damin it,

i was going to post that.... thats the third time you done that pyro ;)

whiteadi
03-15-2004, 08:26 AM
but what, what have I post is not good? I bet you it works well for all browsers

pyro
03-15-2004, 08:29 AM
Originally posted by whiteadi
but what, what have I post is not good? I bet you it works well for all browsers I was simply providing another example - I hadn't tried your code. :)

whiteadi
03-15-2004, 08:39 AM
yeah I was just joking man :)

la_unt
03-15-2004, 08:27 PM
Thank you very very very much!!!
I fixed it!!! :D

m.i.c.
12-17-2006, 06:28 PM
This is maybe an old topic, but i'm really really desperate in trying to get it work.
Because i copy and pasted the code above exactly and it doesn't work :(

I really tried about 20 'force download scripts' and none has worked so far...
But i'm hopefull because at least with this script, i already get a download window, where i can choose if i want to save or open the file, but the problem is that php doesn't find the file, because the download window syas that the size of my jpeg is about 500 bytes, which is way too less of course!

I think it could have something to do with permissions?? Because the php script is in a different directory then the actual picture.

Can anybody help me please because i've really been looking for a solution for this force download thing for days and days if i add all the hours.....

THANKS!!

chazzy
12-17-2006, 08:29 PM
typically, php runs as the apache user. this is usually "nobody"

what are the permissions on the file/directory.

m.i.c.
12-17-2006, 08:41 PM
thanks for your reply.
all the permissions are set to 755, so i don't think that could be the problem.

I have been digging into this problem the last few hours and this is what i discovered :

appearantly the problem is that the 'readfile' function, doesn't have permission to read the pic that i want , 'open_basedir restriction in effect'

I've been looking on the php.net site and i found that the problem might be that the openbase directory is put to the wrong value (i've checked this for my ISP and its value is something with 'temp', so definitely not the directory where my pictures are)

So i think that's why it won't work :(
The solution would be that i have all my pictures in the same directory as this php script, but unfortunately i can't do that.

So i wrote an email to my ISP to ask if and how i can adjust this php.ini file (because appearently it depends from ISP to ISP), so that i can change the openbase dir.

And then hope this solves my problem