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/pdf");
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. 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 |
Request for Question Clarification by
duncan2-ga
on
11 Dec 2002 23:28 PST
Those HTTP headers look correct to me.
Can you tell us which browser versions and builds you're using when
this fails? There are known bugs in various browsers which might
account for this. See, for example, the Microsoft patch for IE 5.5
and 6 for "Incorrect Content-Disposition Handling"
http://www.microsoft.com/windows/ie/downloads/critical/q313675/default.asp
Barring that, do you have any .htaccess files that might be overriding
MIME type defaults?
|
Hello, stever:
I have enjoyed answering this question because also helped me to fix
a problem in some code of mine!! ;-) I work on Linux and I never
tested my code against IE, so I hade the same problem. Your code works
very well with Mozilla, Galeon, etc, but I tested it on Windows XP and
it doesn't worked, I reinstalled Acrobat Reader and the problem was
still there. But the solution was really easy!!
The problem is on the filename you are sending to the browser in this
line:
header("Content-Disposition: inline; filename=$file");
$file is /var/www/arch.pdf which contains a Linux path and IE doesn't
handle very well this kind of file paths, so if you change this line
to:
header("Content-Disposition: inline; filename=downloaded.pdf");
IE will open the document instead asking for download it. Of course
you must have an updated version of IE to prevent the problems
described on this page:
http://www.php.net/manual/en/function.header.php
"Note: There is a bug in Microsoft Internet Explorer 4.01 that
prevents this from working. There is no workaround. There is also a
bug in Microsoft Internet Explorer 5.5 that interferes with this,
which can be resolved by upgrading to Service Pack 2 or later."
So the resulting code is this:
<?php
$file = "/var/www/arch.pdf";
header("Cache-control: private");
header("Content-Type: application/pdf");
header("Content-Length: ".filesize($file));
// It will be called downloaded.pdf
header("Content-Disposition: inline; filename=downloaded.pdf");
$fd = fopen($file, "r");
while(!feof($fd)) {
echo fread($fd, 4096);
}
fclose ($fd);
?>
You can change your code to send the browser the original name of your
file without any path, just the file name.
I hope this solves your problem, it worked for me, but don't hesitate
to request for any clarification if you need anything else.
Regards. |
Request for Answer Clarification by
stever-ga
on
12 Dec 2002 09:16 PST
Hello,
Thanks for your answer. I'm using IE 6 on Windows XP, and I changed
my code to this:
<?php
$file = "/var/www/arch.pdf";
$filename = "arch.pdf";
header("Cache-control: private");
header("Content-Type: application/pdf");
header("Content-Length: ".filesize($file));
header("Content-Disposition: inline; filename=$filename");
readfile($file);
?>
It works fine on Mozilla, but IE 6 still doesn't like it. IE 6
doesn't open a download window, but it doesn't display the document
either -- it just shows a blank page. I would suspect that the PDF
file was corrupted, but it displays correctly in Mozilla, so I know
that's not the problem.
Which version of IE did your code work correctly on?
Thanks,
Steve
|
Clarification of Answer by
joseleon-ga
on
13 Dec 2002 00:13 PST
Hello:
The code I posted was tested using IE 6.0.2600 (the default on
Windows XP) and Galeon 1.0.3, Acrobat Reader for Windows Version
5.1.0. The PHP Version is 4.2.3, could you test this little program on
your webserver and post here the results?
<?php
phpinfo();
?>
This will help me to know if you have something wrong on your PHP
configuration. By the way, the program you post works very well on my
system, both in Linux and Windows. We must check the following things:
-PDF file
Try with another PDF file
Is this file generated on the fly or is static?
Be sure to try first with a static one
-IE version
Try with the XP version if possible
-PHP version
Upgrade to the last version
Post here the phpinfo() output
-Computer
Try with another Windows computer if possible
-Acrobat Reader
Which version you have?
Be sure to update to the last version, uninstall the current version
and install the last one
To be sure it works, check this link, I uploaded your code and a
static PDF and it works on my IE, try it, wait a bit until the pdf is
downloaded and tell me what happens.
http://www.xpde.com/pdftest.php
Regards.
|
Request for Answer Clarification by
stever-ga
on
13 Dec 2002 16:38 PST
The output from phpinfo() is quite long, but this is the part I assume
we care about:
PHP Version 4.3.0RC2
System Linux 2.4.18-14smp #1 SMP Wed Sep 4 12:34:47 EDT 2002 i686
Build Date Dec 6 2002 17:23:00
Configure Command './configure' '--with-mysql=/usr/local/mysql'
'--with-apache=../apache_1.3.27' '--with-openssl'
Server API Apache
Virtual Directory Support disabled
Configuration File (php.ini) Path /usr/local/lib/php.ini
PHP API 20020918
PHP Extension 20020429
Zend Extension 20021010
Debug Build no
Thread Safety disabled
Registered PHP Streams php, http, ftp, https, ftps
The PDF file I'm testing with is static. Acrobat Reader is version
5.1. IE is 6.0.2600.
I tested your http://www.xpde.com/pdftest.php page on my machine, and
it works, so I know it can be done. I'll be working on this some more
later tonight. Thanks for all your help --
Regards,
Steve
|
Clarification of Answer by
joseleon-ga
on
17 Dec 2002 23:57 PST
Hello, stever:
Nice it worked and as you say, sometimes programming is an excercise
of trial and error ;-)
Regards.
|