View saved PDF file in a MySQL database

4

I have a field in a table where I place the PDF file. The type of this field is BLOB.

I would like to know how to display this file on a PHP page, without having to create a directory.

I just want the script to access the field of this table, and show me the file that is there.

    
asked by anonymous 30.01.2014 / 21:13

2 answers

3

Only print the data after changing the content-type of the response:

<?php
header('content-type: application/pdf');
echo "..."; // Só imprimir os dados binários
?>

Optionally, you can define the file name (default):

<?php
header('content-type: application/pdf');
header('content-disposition: attachment; filename="arquivo.pdf"');
echo "..."; // Só imprimir os dados binários
?>
    
31.01.2014 / 18:32
4

You can use the fpassthru() method of PHP to pass the data from your BLOB directly to the PHP that the user is accessing.

link

Example using LOB (Large Object):

<?php
$db = new PDO('odbc:SAMPLE', 'db2inst1', 'ibmdb2');
$stmt = $db->prepare("select contenttype, imagedata from images where id=?");
$stmt->execute(array($_GET['id']));
$stmt->bindColumn(1, $type, PDO::PARAM_STR, 256);
$stmt->bindColumn(2, $lob, PDO::PARAM_LOB);
$stmt->fetch(PDO::FETCH_BOUND);

header("Content-Type: $type");
fpassthru($lob);
?>
    
30.01.2014 / 21:22