468,117 Members | 1,342 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,117 developers. It's quick & easy.

How do we write PHP code to store a file as blob to SQL database?

38 32bit
I've tried the following code, which executes without error, but yet I see nothing in phpMyAdmin to reflect the inserted data:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Require files for server-side includes for fpdf library, functions script, and database connection info.
  3. require $_SERVER['DOCUMENT_ROOT']."/scripts/fpdf/fpdf.php";
  4. require $_SERVER['DOCUMENT_ROOT']."/scripts/functions.php";
  5. require $_SERVER['DOCUMENT_ROOT']."/scripts/connect.php";
  6. //dbinfo is a function defined in functions.php which returns the database connection varriables 
  7. // for $host, $user, $pass, and $dbname. 
  8. dbinfo();
  9. //Connect to database.
  10. $con = mysqli_connect($host, $user, $pass, $dbname);
  11. //check for database connection errors.
  12. if ( mysqli_connect_errno()) {
  13. die('Failed to connect to SQL:' . mysqli_connect_error());
  14. }
  15. //Set PHP vars to write to database.
  16. $postusername = 'testusername';
  17. $postip = getIP();
  18. $postdate = '02/27/21';
  19. $dbfilename = 'test.pdf';
  20. $mime = 'pdf';
  21. $pdffile = fopen($_SERVER['DOCUMENT_ROOT']."/pdf/file.pdf", "rb");
  22. $postcomments = 'insert comments here';
  23. //Prepare and Execute SQL statement.
  24. $stmt = $con->prepare('INSERT INTO pdf (username, ip, date, filename, mime,file, comments) VALUES (?, ?, ?, ?, ?, ?, ?)');
  25. $stmt->bind_param("sssssbs", $postusername, $postip, $postdate, $dbfilename, $mime, $pdffile, $comments);
  26. $stmt->execute();
  27. //Close Database connection.
  28. $stmt->close();
  29. $con->close();
  30. // Give Feedback to User and Exit this script.
  31. echo 'write to database sucessful';
  32. exit;
  33. ?>
  34.  
is this code correct, or should this be done differently?
Feb 27 '21 #1

✓ answered by Niheel

Yes, that's how you would do it.

Assign it to the $var variable and use $var in the SQL statement. Just as you've done.

I've modified the example, thanks for pointing that out.

3 1615
Niheel
2,418 Expert Mod 2GB
Try using
Expand|Select|Wrap|Line Numbers
  1. $file = file_get_contents($_SERVER['DOCUMENT_ROOT']."/pdf/file.pdf")
Use the $file value in the insert SQL statment.

instead of
Expand|Select|Wrap|Line Numbers
  1. $pdffile = fopen($_SERVER['DOCUMENT_ROOT']."/pdf/file.pdf", "rb");
also if you go the fopen route, you are missing the fread & fclose
Expand|Select|Wrap|Line Numbers
  1. $filename = "/usr/local/something.pdf";
  2. $handle = fopen($filename, "rb");
  3. $contents = fread($handle, filesize($filename));
  4. fclose($handle);
Feb 27 '21 #2
bakertaylor28
38 32bit
yes but then how do you pass the file_get_contents( ) to the SQL statement? or would file_get_contents automatically pass to SQL?

It would seem we would effectively need something along the premises of:

Expand|Select|Wrap|Line Numbers
  1. $var = file_get_contents($_SERVER['DOCUMENT_ROOT']."/pdf/file.pdf")
  2. .....
  3. INSERT INTO table foo, foo, blob, foo VALUES ( $foo, $foo, $var, $foo ...); 
  4.  
Feb 28 '21 #3
Niheel
2,418 Expert Mod 2GB
Yes, that's how you would do it.

Assign it to the $var variable and use $var in the SQL statement. Just as you've done.

I've modified the example, thanks for pointing that out.
Feb 28 '21 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Mike John | last post: by
1 post views Thread by Ken Yu | last post: by
2 posts views Thread by Alan T | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.