View saved PDF file in a Mysql database

Asked

Viewed 4,076 times

4

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

I wanted 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.

2 answers

4

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

http://www.php.net/manual/en/function.fpassthru.php

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);
?>

3


Only print the data after changing the reply content-type:

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

Optionally, you can set the (default) name of the file:

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

    In its first example it can be added, because of the doubts (I do not know if it is the default) the header('content-disposition: inline');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.