Hi all,

I'm using PHP 4.2.3 and Apache 1.3 on Linux. I'm trying to create a PHP program which will fetch a PDF file from outside the webserver's document root filesystem and display it to the user inline in the browser. My program so far looks like this:

<?php
$file = "/var/docs/test.pdf";

header("Cache-control: private");
header("Content-Type: application/octet-stream");
header("Content-Length: ".filesize($file));
header("Content-Disposition: inline; filename=$file");

$fd = fopen($file, "r");
while(!feof($fd)) {
echo fread($fd, 4096);
}
fclose ($fd);
?>

It almost works, but not quite. When I load the page (pdftest.php), the browser pops up a window asking if I want to open the file or save it, which is not what I want. On the other hand, if I move the PDF file into the webserver document root directory and link to it directly, it opens with Acrobat in the browser immediately, which is the behavior I'm looking for, but I don't want to store my PDF files in the webserver filesystem, for various reasons.

Is there any combination of headers that will make this program do what I want?

Thanks,

Steve

    Use the appropriate Content-Type for PDFs, so that the browser can know that that is what the document is. I forget what the MIME type is - lemme just check the IANA ... application/pdf it says.

      just a thought, but as you aren't processing the pdf in any way, maybe using

      readfile($filename); //this can include a file path. eg. /var/www/pdfs/file.pdf
      

      Might be better than an fopen and a loop.

        Write a Reply...