Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store a PDF file in MySQL database?

How to store a PDF file in a MySQL database using PHP?

like image 452
user389055 Avatar asked Sep 24 '10 09:09

user389055


2 Answers

Using BLOB (Binary Large Object) (longblob datatype)

$fileHandle = fopen($fileUpload, "r");
$fileContent = fread($fileHandle, $fileUpload_size);
$fileContent = addslashes($fileContent);
$dbQuery = "INSERT INTO myBlobs VALUES ";
$dbQuery .= "('$fileContent')";

The full tutorial available here

but it's strongly recommended to store files on the file system, and just add a reference in the DB (a field with the file path and name). Several reasons:

  • Faster
  • Easier to access (don't need any special application)
  • Faster backups
  • Less space
like image 172
Alex Avatar answered Sep 21 '22 00:09

Alex


Use a type BLOB.

Here's an example in PHP

like image 33
Paul Whelan Avatar answered Sep 23 '22 00:09

Paul Whelan