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 Answer 1

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'];
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your answer but my pdf is still corrupted for some reason ....
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']);?
Also, for storing files you should take a look at this answer
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...
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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.