Save a PDF file created using the FPDF php library in the MySQL BLOB field

I need to create a pdf file with the fpdf library and save it in the blob field in my MySQL database. The problem is that when I try to extract the file from the blob field and send it to the browser for download, the downloaded file is damaged and does not display correctly.

The same pdf file displays correctly if I immediately send it to the browser without saving it in db, so it seems that some data is corrupted when it is inserted into db.

My code looks something like this:

$pdf = new MyPDF(); //class that extends FPDF and create te pdf file
$content = $pdf->Output("", "S"); //return the pdf file content as string
$sql = "insert into mytable(myblobfield) values('".addslashes($content)."')";
mysql_query($sql);

to save the PDF file, and like this:

$sql = "select myblobfield from mytable where id = '1'";
$result = mysql_query($sql);
$rs = mysql_fetch_assoc($result);
$content = stripslashes($rs['myblobfield']);
header('Content-Type: application/pdf');
header("Content-Length: ".strlen(content));
header('Content-Disposition: attachment; filename=myfile.pdf');
print $content;

to send it to the browser for download. What am I doing wrong?

If I change my code to:

$pdf = new MyPDF(); 
$pdf->Output(); //send the pdf to the browser

, , , db.

.

+3
6

(). , escape- .

$content = stripslashes($rs['myblobfield']);

:

$content = $rs['myblobfield'];
+2

:

  • addslashes(), mysql_real_escape_string()

( ):

  • stripslashes()
  • addslashes() mysql_real_escape_string()
+3

, .

$pdf = new MyPDF();
echo $pdf->Output("", "S"); //send the pdf string to the browser
exit;

$pdf = new MyPDF(); //class that extends FPDF and create te pdf file
$content = $pdf->Output("", "S"); //return the pdf file content as string
$sql = "insert into mytable(myblobfield) values('".addslashes($content)."')";
mysql_query($sql);
$sql = "select myblobfield from mytable where id = '1'";
$result = mysql_query($sql);
$rs = mysql_fetch_assoc($result);
$content = stripslashes($rs['myblobfield']);
echo $content; //send the pdf string to the browser
exit;
+1

header("Content-Length: ".strlen(content)); 

header("Content-Length: ".strlen($content)); 

.

+1

!

, . , MySQL PDF blob mediumblob. blob 64 , , PDF , , EOF ( Adobe).

, PDF, , . 64 , , mediumblob.

!

, mysql_real_escape_string() :

  • stripslashes()
  • addslashes() mysql_real_escape_string()

!

0
source

I recently worked with this problem, and it is important to give an answer after the test solutions provided by us. Then I solved this problem as follows. To generate fpdf, I used this code. :

$content = $pdf->Output("", "S"); 
// addslashes is the key to store blob file
$data = addslashes($content); 
$name = "testname.pdf";
$mime = "application/pdf";
$extficheiro = "pdf";
$nomeficheiro = "miarchivo";
$size = strlen($content);
$query = "INSERT INTO filetable(name, mime, size, data, created)
    VALUES ('$name','$mime', '$size', '$data', NOW())";                      
$conn->query($query);

To read (retrieve from the database) from my database, it was preferable to use the pdo code:

if(filter_has_var(INPUT_GET, "var") !== false && filter_input(INPUT_GET, 'var', FILTER_VALIDATE_INT) !== false)
{
    $var = filter_input(INPUT_GET, "var", FILTER_SANITIZE_NUMBER_INT);
    try     {
        $dbh = new PDO("mysql:host=our_host;dbname=database", 'username', 'password');
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $sql = "SELECT * FROM filetable  WHERE id= ".$var;

        $stmt = $dbh->prepare($sql);
        $stmt->execute(); 
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $array = $stmt->fetch();

        /*** set the headers and display the file ***/
        header("Content-Type: ". $array['mime']);
        header("Content-Length: ". $array['size']);/**/
        header("Content-Disposition: inline; filename='". ($array['name'])."'");
        echo $array['data'];
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }
    catch(Exception $e)
    {
        echo $e->getMessage();
    }
}
else
{
    echo '0'; // out of bounds
}

These codes worked great. I hope that this contribution will help others to give up the headache with this problem. Regards.

0
source

Source: https://habr.com/ru/post/1715637/


All Articles