Forum Moderators: coopster

Message Too Old, No Replies

To blob or not to blob

Storing images in mysql

         

one_mind

5:37 am on Oct 29, 2005 (gmt 0)

10+ Year Member



Hi,

I have recently made a vey simple php forum and have allowed users to upload a small 50x50 pixel avatar under 5kb each. I figured it would be better to store them in mysql than mess around with file handling and overwrites ect.

Is it a good idea to use blob in this instance?

I have had no trouble inseting the images into the database following a tutorial but i cant figure out how to display the blob with php and html.

So far i just echo the image and get the following

Ø£Lyu„+šƒ‡5'S\KˆeG8Ò›J’’‘SKf etc etc;

How do i turn it into a picture?

Thnaks for any help :)

[edited by: jatar_k at 2:39 pm (utc) on Oct. 29, 2005]
[edit reason] shortened garbage [/edit]

arran

9:43 am on Oct 29, 2005 (gmt 0)

10+ Year Member



Hi one_mind,

Why don't you store the filepath instead of the actual image? Less hassle and usually faster.

arran.

one_mind

10:12 am on Oct 29, 2005 (gmt 0)

10+ Year Member



Hi,

Thats a good idea.

I ended up getting my way to work and seems fast enough but i'll look into your suggestion.

How would you delete a users avatar if you removed them from your database? My way is just a matter of deleting the users record. Also, since their only avatars and each page will only ever have a max of 10 avatars showing, i think it should be fast enough.

chriswragg

10:17 am on Oct 29, 2005 (gmt 0)

10+ Year Member



If you did want to display this image, you would have to read it in a separate php file, and change the content type to an image. You would also have to store what type of image it was though, as it would not work if you showed it as a wrong image type.

Example code:

<?
/*
MYSQL Code Here
Your image contents have been stored in variable $imgcode
Your image type is stored in $imgtype
*/
header("Content-type: image/".$imgtype);
echo $imgcode;
?>

You would then put the name of this script in a normal img tag.
<img src="img.php?id=1234" alt="Avatar">

henry0

12:17 pm on Oct 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Speed is not you major concern here
Why would you del an user record?
Unless the user opts out.
If not you need to implement an authentication system
Do a query based on ID and Username that will bring up the
User’s icons.
Offer to delete the icons
Or/and a very basic image loader with a max size predefined
All of that is documented in our WebmasterWorld library
Or easy to find on line.
Plus it is a very good PHP training :)