473,418 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

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

45 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 4054
Niheel
2,460 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
45 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,460 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

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

Similar topics

5
by: Norman Fritag | last post by:
Hi there, The Problem: I can't write data to an access database in a webfolder of a windows Prof 2000 machine, whereas the same web application runs fine under Windows XP. I looked into the...
2
by: Mamuninfo | last post by:
Hello all, I want to write java store procedure in db2 universal database by jdbc. After creating the java class, How can i load this class to the database by the sqlj.install_jar() command in...
1
by: Mike John | last post by:
I want to write to text file, the follow text " My Name Is mike" Now, I have used the following sysntax: dim strFilePath as string = "C:\" dim strFileName as string = "MyFile.txt" ...
1
by: Ken Yu | last post by:
Hi, I have one database table to store file (example : PDF, DOC, XLS ...... ) DateType (BLOB), How can download one or more file (same time) from database by asp.net webform ??? tks a lot...
2
by: Alan T | last post by:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="DataBaseConnection" connectionString="server=localhost;database=SalesDB;User
2
by: chrisf01 | last post by:
I am trying to write to a text file and the database at the same time. I have a For statement that loops through all the repeated fields in my form and writes it to a text file. That part works 100%...
10
dmjpro
by: dmjpro | last post by:
Can one explain me in details how to store a text file in a table.For example I have a text file named as aa.txt.I want to store it in a table.Or i can also create a table in order to store it.How...
1
by: ganeshshegde | last post by:
Hi.. Can we store whole file in database? ie it should store in database in same format. .pdf should store in .pdf fomat and .doc should store in .doc fomat.
2
by: simonyong | last post by:
Hello, anyone I had search for few days with how to save file when user choose a file name from listbox and i will search the file from database and user can save it into their desktop what I had...
1
by: hassal | last post by:
Hello, I'm developping a new joomla component, so i want to store pictures in database table in field type : blob. i'm wondery how to proceed to do this. I'm using this to get file from the form...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.