Web Design Forums

PHP

Have questions about PHP? Ask them here and our experts will assist you before you know it! You can also find help in the documentation at PHP.net.

how to insert image to mysql



Site of the Month Nominations
ENTER YOUR SITE NOW!

Reply
 
LinkBack Thread Tools
Old June 14 '09, 06:12 PM (#1)
ketanco is offline
WDF Regular
 
ketanco's Avatar
 
Join Date: October 2007
Posts: 228
ketanco is an unknown quantity at this point
how to insert image to mysql

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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old June 14 '09, 06:17 PM (#2)
ketanco is offline
WDF Regular
 
ketanco's Avatar
 
Join Date: October 2007
Posts: 228
ketanco is an unknown quantity at this point
how to insert image to mysql

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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old June 14 '09, 10:45 PM (#3)
filburt1 is offline
bored
 
filburt1's Avatar
 
Join Date: July 2002
Location: Maryland, US
Posts: 11,785
filburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to allfilburt1 is a name known to all
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old June 15 '09, 09:08 AM (#4)
Dorky is offline
Freelance
 
Dorky's Avatar
 
Join Date: June 2009
Location: Destin Florida
Posts: 905
Dorky will become famous soon enough
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old June 18 '09, 02:56 AM (#5)
Danny[MLWA] is offline
WDF Regular
 
Danny[MLWA]'s Avatar
 
Join Date: June 2009
Location: Goldsboro, North Carolina, USA
Posts: 109
Danny[MLWA] is on a distinguished road
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)
PHP Code:
$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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

  Web Design Forums » Programming Help » PHP

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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

 
User Infomation
Your Avatar

Site Of The Month

Ticket Cake
Ticket Cake

Ticket Cake is a drupal based event ticketing platform. It features that ability to browse events and share them.

Nominate Your Site Now!

Advertisement
WolfCMS.org

Latest Articles
- by RickM
- by bfsog

Advertisement

Partner Links



All times are GMT -4. The time now is 02:48 AM.


WebDesignForums.net is Copyright © 2010 RikeMedia.

SEO by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163