Hi,

I hope someone has run into this before. I have a new Web app I'm developing; it currently only runs on my machine, using XAMPP as the testing server. I created a folder outside of the web root, called '_pdfs' to house PDFs which will be uploaded by users; once the PDFs are approved, users will be able to see the titles presented on a PHP page in a table of hyperlinks, which can be clicked to obtain the file.

Following instructions found elsewhere, I included a function called 'send_download()', included here:

function send_download($filename)
	{
	$file_path = '../_pdfs/' . $filename;
	$file_size=@filesize($file_path);
	header("Content-Type: application/pdf"); 	
	header("Content-disposition: attachment; filename=$filename");
	header("Content-Length: $file_size");
	readfile($file_path);
	exit;
	}

Here's where it breaks: the table with the links displays properly, and apparently sends the user to the correct page, which calls the PDF. Adobe Reader then loads, and asks if the user wants to open or save the file (and the name it gives is correct). However, when it tries to open the PDF, it stops and instead displays this error message:

Adobe Reader could not open '12345_upload_test.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded.

I believe it's finding the file correctly, but wonder if there is a header problem or such.

Thank you for your assistance.

    If you save the PDF file instead of opening it, is the file that your browser downloads the same size as the PDF on the server?

    Also, do you have error_reporting set to E_ALL and log_errors set to On?

      Hi,

      No, actually the file which the browser tries to open or save is not the same size as the one in the storage folder - it only reads as 1KB, whereas the test PDF is 81 KB.

      I have gone into the php.ini file and set the values for error_reporting to E_ALL and log_errors set to On (writing to a file called "C:\xampp\apache\logs\php_error_log.txt")- however, it doesn't seem to be writing anything into that file yet.

        Update:

        OK, I think I found the error- I was barking up the wrong tree, thinking that this was a problem with the server or the headers. It's a problem finding a folder which is outside the Web root.

        The code I'm using to find and read the file is-

        function send_download($filename)
        	{
        	$file_path = '../_pdfs/' . $filename;
        	$file_size=@filesize($file_path);
        	header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        	header("Content-Type: application/pdf"); 	
        	header("Content-disposition: attachment; filename=$filename");
        	header("Content-Length: $file_size");
        	readfile($file_path);
        	exit;
        	}
        

        When I use a Web form to upload the PDF, it uploads smoothly to a folder which is outside the Web root. But I'm having problems allowing the user to, essentially, find and download the file from outside the Web root. Can anyone suggest or show an example of how to reach a file in this outside folder?

        Thanks!

          Ok, problem solved: I was able to upload the PDF to the proper folder, but I was not then linking to the correct (out-of-web-root) folder to download the PDF into the browser.

          Thanks for pointing out that the downloaded file did not have the same file size as what was stored in the folder: one thing I learned is to be careful of is the use of "@" to suppress error reporting.

            akeyworth wrote:

            to be careful of is the use of "@" to suppress error reporting.

            The rule of thumb to keep in mind when using the '@' error suppressor is this: don't use it. 🙂

              Write a Reply...