Page 1 of 1

Newbie Question - Storing Images

Posted: Wed Mar 19, 2003 3:26 pm
by ScotlandOnLine
Hi Forum -

Just a quickie question - has any body got any code that they wish to share with me that will allow images to be stored in Mysql Database ?

What type of database object is required to store images and which PHP functions can be used to store/retrieve such?

Thank s in advance for your help !

Posted: Wed Mar 19, 2003 3:35 pm
by daven
I generally advise against storing images in the database directly. It is better to store the images in a folder, and have a reference to them (such as the filepath) in the database.

That being said, you need to use a BLOB (binary large object) column type to store images in a DB

Posted: Wed Mar 19, 2003 4:08 pm
by JPlush76
I'll second that

Posted: Wed Mar 19, 2003 4:25 pm
by ScotlandOnLine
Which bit jim ?

the fact that dave is a bottom feeder - lol

-or don't store the images in the database directly?

Posted: Wed Mar 19, 2003 4:26 pm
by JPlush76
lol not to store images in a database :)

its better design to just store a path... so in your database table you would have a field called "image" and just put /images/picture.jpg

:)

I'm sure Dave only feeds from the top :lol:

>>Jim

Posted: Wed Mar 19, 2003 4:33 pm
by ScotlandOnLine
so how do I code the transfer of the image from the users browser to get it on to my disks .... without letting the user see the other content (images) in the target directory .....I guess the file upload function is out!


and then dynamically embed the image from the path if it needs to be displayed
... I see
...I think....

Posted: Wed Mar 19, 2003 4:35 pm
by JPlush76
you create a file upload form

user submits image from their harddrive

you test the form to make sure they are sending an image and its under the size you want

the you move the file to a directory on your server

search around here for this, there have been code samples before on this in the forum.

Posted: Wed Mar 19, 2003 9:52 pm
by daven
viewtopic.php?t=6435
Page with lots of stuff about file uploading scripts.

Hmm...note to self: ask Jason if he is willing to make a sticky about file uploading, since we seem to get a lot of questions about it.

And Scotland: You get major points for knowing that "benthic riparian algal feeder" means bottom dwelling scum sucker. *grin* I get lots of questions about it.

Related note: my name is Daven, not Dave. I know way too many people named dave to permit myself to be referred to as one. Cheers. (yes, I have been at work WAY too long today)

Daven

Posted: Thu Mar 20, 2003 12:04 pm
by ScotlandOnLine
Many apologies for the name mistake - read and digested !

as for reference the benthic riparian algal feeder - I guess you get called far worse *wink*


Thanks for the link though - very useful!

Posted: Thu Mar 20, 2003 7:48 pm
by fractalvibes
A caveat to all the above, I think - It Depends. If you are uploading images that only 1 app will use - store the link to the image, not the image in the database. I have some situations where applications on different servers (Internet vs Intranet) need access to these photos. As the two sites cannot see each other's file systems, they can see the same database, therefore that is where the images reside. If the database is well-tuned and you can access the image by a unique primary key - say, employee number - performance is very good. A BLOB column can store anything from a jpeg to an MP3 file, so you could create a universal media
serving app very easily - just save the mime type along with the blob and you can serve up any media!

But, if it is a simple app - do as the others wisely say.

My 1/2 cent...

Phil J.

Posted: Fri Mar 21, 2003 12:29 am
by phice
What I would theoretically do is:

1) First, md5(PICTURE_FILENAME), for fun.
2) Copy the name of that, store it inside a database.
3) When viewing the picture, call the picture name from the database, and it should give you the correct picture.

(note: md5 isn't needed at all, but I seem to think that having all file names the same length tends to be more neat :/)