Web Design Forums

Welcome! Please register or log in: Forgot your password? Why register?
You are here: Web Design Forums » Programming Help » PHP » how to insert image to mysql RSS

how to insert image to mysql

This thread was started by ketanco and has been viewed 1011 times, and contains 4 replies, with the last reply made by Danny[MLWA].
Post Reply
1
View ketanco's reputation
ketanco, Must... Post... More...! Private message   E-mail
Posted June 14 '09 at 06:12 PM
      Posts: 228
Hello,
How do you insert image to mysql database? I prefer this to avoid uploading image to a dir and also to have all my text and image in just one place which is the database, but I heard that inserting image to mysql database is not recommended. Why might that be?
Also can I use
<input name='new_image' id='new_image' type='file' />
to load the image to mysql database?
If not, what is the recommended way?

Advertisement Register for free to hide these ads and participate in discussions!

2
View ketanco's reputation
ketanco, Must... Post... More...! Private message   E-mail
Posted June 14 '09 at 06:17 PM
      Posts: 228
Hello,
I have several questions about inserting image to a mysql database:
1-I prefer inserting image to mysql database to avoid uploading image to a dir and also to have all my text and image in just one place which is the database, but I heard that inserting image to mysql database is not recommended. Why might that be?
2-How can I do this - insert image to database?
3-Regardless of the answer to question 2, can I use<input name='new_image' id='new_image' type='file' /> as well to load the image?
4-Finally, to what type of datafield would I insert the image?

3
794 points at 99% Donor Moderator Repute WDFplus Member
Posted June 14 '09 at 10:45 PM
      Posts: 11,793
Use a column type that supports binary data (IIRC, in MySQL, this is BLOB; in PostgreSQL, it's BYTEA).

How file uploads work in PHP: http://us3.php.net/manual/en/features.file-upload.php

Usually storing a few dozen or hundred kilobytes of file data in a single row is fine in most database engines. More than that and there are many complications, among which is memory usage (usually you have to buffer the entire row into RAM and then send it to the browser instead of stream it directly from the database). Most database engines, including MySQL, also impose maximum query sizes which can limit the data going into the table.
filburt1, Web Design Forums.net founder
Want to advertise on this site?
Site of the Month contest: submit your site or vote for the winner!

4
44 points at 77%
Dorky, Freelance Home page   Private message  
Posted June 15 '09 at 09:08 AM
      Posts: 790
yes this can be complicated and really slow things down so if you must do this then also do things like strip exif data where possible and anything you can to reduce file size.
“Imagination is more important than knowledge. For knowledge is limited to all we now know and understand, while imagination embraces the entire world, and all there ever will be to know and understand.” Albert Einstein

5
32 points at 100%
Posted June 18 '09 at 02:56 AM
      Posts: 76
I've done this before just fine though it is only recommended with a LARGE amount of smaller images or in cases where you have or may have a lot of people editing images on your site (using gd or image magick).

For as to how you do it, you would load the $_POST['image'] dirrectly into the database same as you would if it was a text field except you have to run stripslashes() on the data first:

(ensure the data field is set to BINARY)
$query = ("INSERT INTO `table` (`id`,`name`,`binary`) VALUES (`" $count "`, `"$_POST['name'] ."`, `"stripslashes($_POST['image']) ."`)"); 

You would pull it out with a separate php file that sends the image header along with the echo from the binary field.
Lead Web Designer
MLW & Associates, LLP.
W3C Standards Complaint and SEO Friendly
Admin of Vraul

Post Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
Redirect before mysql insert.. but after submit AndrewWest MySQL and MaxDB 5 July 15 '04 06:10 PM
MySQL syntax question (INSERT INTO...) xarst MySQL and MaxDB 6 June 5 '04 09:03 AM
Simple insert change (MySQL) teal MySQL and MaxDB 3 April 11 '04 02:01 AM