Display / Download file



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");

}
.



Relevant Pages

  • Re: pdf in java Viewing... Not a good experience
    ... > BUT I need to know the external pdf viewers name ... This code is austensibly to open a url in a browser, but on Windows it will ... static class to display a URL in the system browser. ...
    (comp.lang.java.gui)
  • Re: EU nations refuse to force members to farm GM maize
    ... the Spiegel logo is on the Business Week page. ... Since there are security flaws in PDF files, allowing them to be opened to ... they don't display consistently across browsers. ... doesn't have a plug-in to show the PDFs in the browser but that's just ...
    (misc.rural)
  • Re: Display / Download file
    ... use a showDoc.php file to display the file. ... want this to be in the browser. ... xls - when the link is clicked, I get an excel warning that the file ... If I click 'open this file anyway, it opens excel and ...
    (alt.php)
  • Best way to embed a document in an MFC Application
    ... somewhere in the view i need to display a document. ... pdf, an xls or an image. ... what method would yuou pick? ...
    (microsoft.public.vc.mfc)
  • Re: Soaring Magazine 1937 through 1967 available online
    ... I don't know anything about computers, but wouldn't you use a .pdf ... I had to change Foxit, my pdf reader, to "display in browser" before the pages would work. ... This is quite workable, but it means I have to change the setting on my browser from my normal "display in separate window" to view the Soaring pages, then change back to my normal preference. ...
    (rec.aviation.soaring)