SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,617
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Just save the generated content in a PHP variable. Then, use a function to determine if "magic quotes" are on or off. If off, run the content through the addslashes() function to escape it.

    PHP Code:
    if(get_magic_quotes_gpc()==false)
    {
        
    $contentaddslashes($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.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,871
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,617
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by felgall View Post
    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.
    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.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  5. #5
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,446
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Hi jonito, welcome to the forums!

    Quote Originally Posted by jonito View Post
    I want to know how to retrieve the content and put into pdf file?
    It should just be a case of querying the DB and putting the PDF contents into a variable, then you can use the file_put_contents function to write it to a file:
    PHP Code:
    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:
    PHP Code:
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •