Save and retrieve content pdf as blob

Hi everybody…I’m a serious problem with my code.I need to save the content or the file pdf with the content into database.
I use FPDF to generate my content and I save it like MEDIUMBLOB.
I don’t want to save the pdf file into diretory system.

I want to know how to retrieve the content and put into pdf file?

Thanks

Just save the generated content in a PHP variable. Then, use a function to determine if “[URL=“http://www.php.net/manual/en/info.configuration.php#ini.magic-quotes-gpc”]magic quotes” are on or off. If off, run the content through the [URL=“http://php.net/manual/en/function.addslashes.php”]addslashes() function to escape it.

if(get_magic_quotes_gpc()==false)
{
    $content= addslashes($content);
}

Then, use an INSERT statement to save it to the database–pretty much the same as any other data type.

Note that it is better to save a URL to the file in the database, rather than the file itself, since saving files in a database creates bloat and reduces performance.

Never use the addslashes() function to escape data being put into a database. The preferred way to do it is to use prepare and bind to keep the data separate so it doesn’t need to be escaped. If you can’t avoid jumbling the data with the SQL then use the database specific function to escape the data rather than the PHP one.

It is not always better to save a URL in the database rather than the complete file. It all depends on the actual requirements. When stored separately you lose all of the security on file access that the database provides and also lose the built in data integrity where the database maintains the relationship between the file content and the other data. For example, i for any reason a delete process needs to do a rollback it can’t undelete the file if it isn’t stored in the database and so integrity is lost as everyything except the file is rolled back to how it was before. Whether the “file” should be stored separately or stored in the database depends on which of the various pros and cons to the two alternate approaches are most important in the specific circumstances.

Agreed. I made an assumption as to what the user was trying to accomplish.

It is not always better to save a URL in the database rather than the complete file. It all depends on the actual requirements. When stored separately you lose all of the security on file access that the database provides and also lose the built in data integrity where the database maintains the relationship between the file content and the other data. For example, i for any reason a delete process needs to do a rollback it can’t undelete the file if it isn’t stored in the database and so integrity is lost as everyything except the file is rolled back to how it was before. Whether the “file” should be stored separately or stored in the database depends on which of the various pros and cons to the two alternate approaches are most important in the specific circumstances.

Good point. I made another assumption here as well and tried to offer general rule-of-thumb advice.

Hi jonito, welcome to the forums!

It should just be a case of querying the DB and putting the PDF contents into a variable, then you can use the [fphp]file_put_contents[/fphp] function to write it to a file:

file_put_contents('myfile.pdf', $pdf_contents);

If you want the user to be able to download the PDF, then probably what you want to do is something like this:


header('Content-type: application/pdf');
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("Content-Disposition: inline;filename=myfile.pdf'");
header("Content-length: ".strlen($pdf_contents));

echo $pdf_contents;

which will set the correct headers and output the file to the browser. If the user has a PDF reader plugin, they’ll be able to view it in their browser, otherwise they’ll be prompted to download it.