0

Hi so I am wondering where to start on uploading a pdf to a sql database. I am trying to make it so that a user can upload a file and then hit submit and that file is stored in a cell on a sql database. Is this the correct way to solve this problem? I do not need to parse the pdf at all, I just need to be able to upload it and pull it back out of the database. Thank you!

3
  • 2
    the best way is to upload the file on your server and store the file name on the database Jan 23, 2014 at 17:13
  • Do you have a database yet? If so, how did you organize it i.e. what is the schema?
    – jacobq
    Jan 23, 2014 at 17:14
  • You really want to store whole PDF within database? or just store path of the pdf in one cell & access the pdf in your application using that path? Jan 23, 2014 at 17:15

2 Answers 2

1

You can store the PDF on the server or in a database. If you need it to be stored in a database, use the following:

$fileAsString = base64_encode(file_get_contents('PDF.pdf'));
//now store $fileAsString in your database cell

When you need to read it again, you use base64_decode();

2
  • So how would the user go about uploading the file, for instance if they had a pdf on their desktop. I would just use a form to create the pointer to the pdf and then when they selected attach, then I would run the sql query?
    – vector
    Jan 23, 2014 at 18:07
  • You would upload the file as usual. PHP will create a temporary file for you, which you could read into the base64_encode() function, then take that string and save it to the DB.
    – Kovo
    Jan 23, 2014 at 18:25
1

If you really want to store pdf (as BLOB) in the database, (Assuming MySQL functions are being used to interface with the database) You could do something like this:

$store_pdf =  mysql_query("INSERT INTO table (data) VALUES ('".mysql_real_escape_string(file_get_contents('/pdf/file/path.pdf'))."')");

Note:

Storing BLOBs in databases is generally not considered. A better approach would be to move the file somewhere in the filesystem and store the path to the file in the database instead of the file itself.

Eg.

$path_to_pdf='/pdf/file/path.pdf';

mysql_query("INSERT INTO table (pdf_path) VALUES ('$path_to_pdf')");

& Access pdf file after fetching the path from the table.

<?php 
$query=mysql_query("SELECT pdf_path FROM table");
$result=mysql_fetch_array($query);
?>
<a href="<?php echo $result['pdf_path']; ?>">Download PDF</a>

Note that the mysql extension is now deprecated and will be removed sometime in the future. You could use PDO functions instead. (http://www.php.net/manual/en/book.pdo.php)

2
  • So how would the user go about uploading the file, for instance if they had a pdf on their desktop. I would just use a form to create the pointer to the pdf and then when they selected attach, then I would run the sql query?
    – vector
    Jan 23, 2014 at 18:07
  • @visualbasicNoob9000 If you're storing pdf content within DB, you don't need to upload the file. You just read the file contents & store it in the field. But it's not recommended instead just upload the file & store only the path within DB table field (Yeah..Running the SQL Query) Jan 23, 2014 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.