PHP-unable to download a .pdf file from database , file content is stored in mysql database
mysql, pdf, php
Solution
The main reason why `page is changing into gray colors` is that the browser is unable to detect content type correctly.
Try this:
header("Content-type: $mimetype");
header('Content-Disposition: inline; filename="'.$question.'"'); // Filename should be there, not the content
Instead of :
header('Content-type: $mimetype');
header('Content-Disposition: inline; filename="$question"');
It seems that you have invalid quotes, so the content type is not specified correctly.
EDIT
To clear out, let's assume that `$question` is the binary PDF content. That is what your code should be:
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename=anything.pdf');
header('Content-Transfer-Encoding: binary');
echo $question;
ERRORS EXPLAINED
Let's discuss your original code and your errors.
$mimetype = 'application/pdf';
$disposition = 'attachment';
// First error: you have single quotes here. So output is 'Content-type: $mimetype' instead of the 'Content-type: application/pdf'
header('Content-type: $mimetype');
// Second error. Quotes again. Additionally, $question is CONTENT of your PDF, why is it here?
header('Content-Disposition: inline; filename="$question"');
header('Content-Transfer-Encoding: binary');
// Also bad: strlen() for binary content? What for?
header('Content-length: ' . strlen($question));
header('Accept-Ranges: bytes');
echo "$question";
ONE MORE EDIT
i have another query...i want to change the filename into $year.pdf..$ year may have values like 2007..how can i do that???
Try this :
$year = '2013'; // Assign value
header('Content-Disposition: inline; filename='.$year.'.pdf');
Instead of:
header('Content-Disposition: inline; filename=anything.pdf');
Problem
i have written a code in php which will echo a pdf file.whenever i am trying to echo that pdf,the browser page is changing into gray colors and the loading icon at the bottom left side corner is appearing and after that it fails to show that pdf file. what i can assure you that code upto fetching the data from databse is perfect.there is no error or mistake.after fetching the data i have used following headers to echo that file.i am not sure about these headers. ``` $mimetype = 'application/pdf'; $disposition = 'attachment'; header('Content-type: $mimetype'); header('Content-Disposition: inline; filename="$question"'); header('Content-Transfer-Encoding: binary'); header('Content-length: ' . strlen($question)); header('Accept-Ranges: bytes'); echo "$question"; ``` NOTE: I have used .pdf extension in content-decomposition.but that was not fruitful to me.also used readfile() function and it was also not helpful to me. Can anyone tell me what's wrong there?