GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Mar-2009, 16:06
hellz hellz is offline
New Member
 
Join Date: Mar 2009
Posts: 3
hellz is on a distinguished road

Images in MySQL and PHP


hi,

This is prob really easy but im trying to store a image in mysql in a blob. Not aure how to put the image in there to start with and then i want to display the image using php on a webpage including other information from the database. I can get it to show all the text information but no image.

Any help would be greatly appriciated
  #2  
Old 23-Mar-2009, 21:01
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Images in MySQL and PHP


There is nifty example of doing what you seek here. (follow the $data variable where they do the insert from the upload -- and note the size limitation section near the bottom)

Depending on the scope of what you're trying to do, at our shop, we've avoided using blobs in some instances, and simply store a path-with-filename string (as a varchar) instead. [this could be further simplified to a constant path, and only storing a file name]

It seemed sort of wasteful, for some of our uses, to keep images stored on the harddrive and also have the additional storage overhead for 'blobs' in the database. But again, this would all depend on the overall goal/scope of your project.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 24-Mar-2009, 12:22
MisterChucker's Avatar
MisterChucker MisterChucker is offline
Junior Member
 
Join Date: Mar 2009
Location: Cyberspace, Earth
Posts: 53
MisterChucker is a jewel in the roughMisterChucker is a jewel in the roughMisterChucker is a jewel in the rough

Re: Images in MySQL and PHP


I prepared this response earlier and would have posted it sooner if not for some problems with my activation email. So, for what it's worth...

The short answer:

To store the image: use file_get_contents() and treat the image data as a string. Use mysql_query() to insert the data into your database. The field your image data is stored in should be of type TINYBLOB, BLOB, MEDIUMBLOB, or LONGBLOB, depending on the size of images you want to store.

To retrieve the image: query the database then convert the image data into a usable image using imagecreatefromstring(). Output the image using header() and imagejpeg().

To embed the image in a page along with other HTML elements: use the img tag and point it to a script that retrieves and outputs the image. For example:
HTML Code:
<img src="script.php?id=42" alt="" />

The long answer:

See this example: misterchucker.com/image_blob_test

Other considerations:

I get an error 2006 "MySQL server has gone away" when I attempt to store images larger than 1MB. For more on this, see dev.mysql.com/doc/refman/5.0/en/gone-away.html. An alternative to messing with the server configuration is to resize the uploaded image using imageresize() before you send it to the database.

Another option would be to store your image files on the filesystem and store just their paths in the database.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
RESELLER WEB HOSTING - $9.99/MONTH For 4GB HD & 30GB BW! CPanel, PHP, MySQL & MORE! IncognitoNet Web Hosting Advertisements & Offers 0 30-Jan-2005 22:46
RESELLER WEB HOSTING - $9.99/MONTH For 4GB HD & 30GB BW! CPanel, PHP, MySQL & MORE! IncognitoNet Web Hosting Advertisements & Offers 0 11-Nov-2004 20:51
PHP, MySQL, WML skyloon MySQL / PHP Forum 0 05-Mar-2004 08:53
Windows: From only £20p/y,Linux: from $10p/m. ASP, ASP.NET, PHP, Free MySQL, +More EyotaHosts Web Hosting Advertisements & Offers 0 28-Jun-2003 14:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:10.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.