Trouble Downloading Files
| From: | Roger Lewis | Date: | Sun, 25 Aug 2002 00:45:14 +0000 |
| Subject: | Trouble Downloading Files | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-113989@lists.php.net to get a copy of this message | ||
Hi,
I'm not certain this is the right place to ask, but here goes. For security
reasons I have placed certain document files in a directory outside my web
root. There are many types of files included (e.g. jpg, gif, txt, pdf, xls,
doc, mdb, ppt, and dwg). Clicking links on my web page will download the
files through a php script (getdocfile.php) into a new pop-up window. The
following is how I have coded the link:
<a href='#' target="_blank"
onclick="window.open('../getdocfile.php?file=<?php echo
$DrawingGroups->Value("filename")?>',
'_blank','resizable=yes,scrollbars=yes,width=700,height=500');return
false"><?php echo $DrawingGroups->Value("is_viewable")?></a>
Problem: The download fails with IE5.5 when trying to retrieve files other
than jpg, gif, txt, and pdf. One of the following happens depending on
whether or not I force the download dialog box for the file to appear:
(Note: I force the download dialog by commenting out the appropriate mime
types in the code below)
If I do not force the dialog box to appear
1. The applications (i.e. xls and doc) open in new window but with a blank
screen, i.e. no data. For example, Excel will open with a new sheet but
with nothing in it. If IE could open Excel, what did it do with the data.
2. A new blank window opens and then hangs and locks up. (This for ppt, mpp,
and mdb)
3. The file open-or-save dialog appears, and then the system hangs
If I force the dialog box to appear
1. The file open-or-save dialog appears, and then error msg: IE cannot
download file
The following code works without problems in NN4.7 and NN6.2. Also, when
putting the files in a directory inside the web root, I have no problem
downloading them with IE5.5; however, I am then not using getdocfile.php to
download the files. Does anyone have any idea as to why this might be
happening or where else I might look for a solution? I am also trying an
IE5.5 newsgroup and have been searching the php archives, but so far no
luck.
Any help is much appreciated.
Roger Lewis
**********************START CODE***********************************
<?php
// getdocfile.php
include("accesscontrol_gen.php");
define('FILEDIR', '/home/sites/home/users/demodocs/docs/ProjDocs/');
$path = FILEDIR . $file;
//check that this file exists and that it doesn't include
//any special characters
$filename = substr($file, strrpos($file,"/")+1);
if(!is_file($path) OR !eregi('^[A-Z_0-9][A-Z_0-9.]*$', $filename))
{
header("Location: error.php");
exit();
}
$mimetype = array(
'doc'=>'application/msword',
'htm'=>'text/html',
'html'=>'text/html',
'jpg'=>'image/jpeg',
'gif'=>'image/gif',
'pdf'=>'application/pdf',
'txt'=>'text/plain',
'xls'=>'application/vnd.ms-excel',
'ppt'=>'application/vnd.ms-powerpoint',
'mdb'=>'application/vnd.ms-access',
'mpp'=>'application/vnd.ms-project',
'dwf'=>'drawing/x-dwf',
'dxf'=>'image/vnd.dxf',
'dwg' => 'image/vnd.dwg',
'zip'=>'application/x-zip-compressed',
'wav'=>'audio/wav'
);
$p = explode('.', $file);
$pc = count($p);
//send headers
if(($pc > 1) AND isset($mimetype[$p[$pc - 1]]))
{
//display file inside browser
header("Content-type: " . $mimetype[$p[$pc - 1]] . "\n");
}
else
{
//force download dialog to appear
header("Content-type: application/octet-stream\n");
header("Content-disposition: attachment; filename=\"$file\"\n");
}
header("Content-transfer-encoding: binary\n");
header("Content-length: " . filesize($path) . "\n");
//send file contents
$fp=fopen($path, "r");
fpassthru($fp);
?>
***********************END CODE ***************************