Display / Download file
- From: tomhawkins1@xxxxxxxxx
- Date: Sun, 8 Jun 2008 05:07:28 -0700 (PDT)
Hi
I currently store all uploaded documents outside the web root and then
use a showDoc.php file to display the file.
What I want is that doc, pdf, xls and dwf (all users will have an
installed viewer) to display in the browser with dwg and dxf being
downloaded. So far I have the following code but it's not behaving
exactly as I want:
doc - the link is clicked, word is opened and displays the file. I
want this to be in the browser.
xls - when the link is clicked, I get an excel warning that the file
extension of showDoc.php is different to the content being presented
(ie xls). If I click 'open this file anyway, it opens excel and
displays the file. I would like this to be in the browser.
pdf, dwf, dwg and dxf all do what I want.
Any words of wisdom?
Cheers
TomH
---------------------------------------------------------
function readfile_chunked ($filename) {
$chunksize = 1*(1024*1024); // how many bytes per chunk
$buffer = '';
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
print $buffer;
}
return fclose($handle);
}
$filename = $uploaddir . $client . "_" . $proj . "_" . "$doc" . "." .
$type;
//This results in c:/stuff/uploads/11_20_30.doc for example
$file_extension = strtolower(substr(strrchr($filename,"."),1));
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
switch( $file_extension )
{
case "pdf": $ctype="application/pdf"; $forceDownload = 0; break;
case "doc": $ctype="application/msword"; $forceDownload = 0; break;
case "xls": $ctype="application/vnd.ms-excel"; $forceDownload = 0;
break;
case "dwf": $ctype="application/x-dwf"; $forceDownload = 0; break;
case "dwg": $ctype="application/x-dwg"; $forceDownload = 1; break;
case "dxf": $ctype="application/x-dxf"; $forceDownload = 1; break;
}
if($forceDownload ==1){
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=
\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile_chunked("$filename");
exit();
} else {
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: inline; filename=
\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile_chunked("$filename");
}
.
- Follow-Ups:
- Re: Display / Download file
- From: J.O. Aho
- Re: Display / Download file
- Prev by Date: Re: Calling a function within a Post Form
- Next by Date: Re: Display / Download file
- Previous by thread: Calling a function within a Post Form
- Next by thread: Re: Display / Download file
- Index(es):
Relevant Pages
|