Link to home
Start Free TrialLog in
Avatar of navtarainc
navtarainc

asked on

Read PDF file from outside of public_html folder

Hi all,

     I am facing a problem with the read the PDF  file  outside of public_html folder i.e
/home/rakesh/ramteke/abc.pdf
 can anybody suggest how many ways to do it using php script. I tried it by readfile() function but it damage the pdf file by putting %PDF  to the beging of the file. and acrobat anable to open this file.

Thanks in advance
Avatar of walkerke
walkerke

There are a couple of problems with your question. For one, every PDF file begins with %PDF-1.x where 1.x would be the PDF version number. Secondly, readfile() does not make changes to a file, it simply reads a file. If your file was damaged by the script, it was not by putting %PDF at the beginning nor was it the result of a readfile(). There must be something else in your script that caused the file to get corrupted.

What do you intend to do with the PDF codes once you've read them with the readfile()? Reading a PDF using readfile() will read the ASCII codes within PDF file. If you wish to serve the PDF file to the HTML stream, this is not the correct method.
moreover the HTTPD user would not have read rights within a user's home directory.  
@walkerke readfile is binary save and preseves binary data.. SO readfile is one option to output file conetent to the http stream.
Other option would be to use fpassthru like

$name = '/home/rakesh/ramteke/abc.pdf';
$fp = fopen($name, 'rb');
// dump the picture and stop the script
fpassthru($fp);
I stand corrected.
Avatar of navtarainc

ASKER

Thanks for your suggestions but problem was different one
i.e
   if    /home/rakesh/ramteke/……/abc.pdf is the path for the pdf file
$pathname =$_GET['reportname'];
      
      $filename="/home/updev/dms_reports/QuotingTool/bin/".$pathname;
      file_exists($filename)or die("No File available");
      header("Content-Type: application/pdf");
      $fp = fopen($filename, 'rb');
fpassthru($fp);

Now, I changed the way i.e

$ filename =$_GET['reportname'];
      file_exists($filename)or die("No File available");
      header("Content-Type: application/pdf");
      $fp = fopen($filename, 'rb');
fpassthru($fp);

and it works

Thanks again for u r help
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial