Page 1 of 1

PDF File Download with PHP Auth

Posted: Tue Oct 17, 2006 10:04 am
by jcherry
Hi,
I am working on a project to convert a directory of PDF's from using HTTP authentication to use an integrated cookie based login, built with PHP.

My solution for this was to move the PDF's outside the web root, then check authentication in my php script, and then display the PDF.

A simplified/testing version of the script follows (authentication has been removed for testing since it is not relevant to the problem):

Code: Select all

$file = "/path/to/file.pdf";
header("Content-Type: application/pdf");
header("Content-Length: ".filesize($file));
header("Content-Disposition: inline; filename=\"".basename($file)."\"");
readfile($file);
With this method, and many variations of this method, the PDF will not display.
I have tried:
Using readfile(), include(), fread(), and file_get_contents() to retrieve the PDF data
Using the content-types of: application/pdf, application/octet-stream, application/force-download

Various errors are returned including:
"The file has been damaged and cannot be repaired"
"The file does not begin with '%PDF-'"
"The file type specified is incorrect"

When the file is viewed directly (through Acrobat or a web browser (Firefox 1.5/2.0 Win/OSX, IE 6/7, Opera/OSX)), it displays fine.

I have tested several different PDF's, including the ones I am developing for and miscellaneous PDF's from the web. None work. These PDF's are fairly large, ranging from 2-6MB. The one thing I have not tested is handling the file with a partial content/byte range download. I have not tested this because when I extract just the first page of one of the sample PDF's, to reduce the file size to just a few hundred KB, the same errors occur. So I don't think the problem is occurring because of the file size.

At this point, I have no idea what the problem here is. I feel like I have tried everything I can think of. Also I have found limited information on the internet regarding this type of problem.

Has anyone else run into this problem? Or has anyone implemented this type of solution? Does anyone know a different way to implement what I am trying to do?

Any help is greatly appreciated!!

Thanks,
Johanna