0

I need help with this script. I'm trying to get this binary file stored in a sql server DB . The main problem is that , each time im trying to show it in my browser or to download it, the file is corrupted . Heres my code:

       $binary = $row['PDF_FILE_STORED'];
        file_put_contents('my.pdf', $binary);
        header('Content-type: application/pdf');
        header("Content-Transfer-Encoding: Binary"); 
        header("Content-Length: ".filesize($binary));
        header("Content-Disposition: attachment;filename=my.pdf");
        ob_clean();
        flush();
        echo $binary;

Is there a problem with the encoding aspect? I just got this warning in my brower's console : "Resource interpreted as Document but transferred with MIME type application/pdf" . Any advices ?

1 Answers1

1

To just download the pdf file, you don't need to save it locally on the server. You don't need to send content-length as that should be done automatically and I would skip the content-transfer-encoding as well.

If there was no output yet, you can also skip dealing with the output buffer.

Try this:

header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=my.pdf');
echo $row['PDF_FILE_STORED'];
Peter Rakmanyi
  • 1,475
  • 11
  • 13
  • Thanks for your answer but my pdf is still corrupted for some reason .... – Random_Display_Name Jul 17 '18 at 07:38
  • What does it look like when you read it from the db? Is it corrupted if you `file_put_contents('my.pdf', $row['PDF_FILE_STORED']);`? – Peter Rakmanyi Jul 17 '18 at 11:03
  • Also, for storing files you should take a look at this [answer](https://stackoverflow.com/a/13421029) – Peter Rakmanyi Jul 17 '18 at 11:08
  • In the db i got a binary file, when i try to use file_put_contents('my.pdf', $row['PDF_FILE_STORED']); , it is still corrupted (bad encrypted error from adobe) . But i got an .net app that can read it properly and download it . So actually i don't know whats the main problem... – Random_Display_Name Jul 17 '18 at 11:55
  • There must be a difference in how it is read by the other .net app and this script. Can you check what query is used in each one and compare? – Peter Rakmanyi Jul 17 '18 at 13:50
  • Have you solved the problem? If not be sure you don't have any white spaces outside php code. I will be dealing with project like that soon. Also check if you get whole binary string from database (it might be limited to some lenght) – baron_bartek Aug 18 '22 at 18:40