Newbie Question - Storing Images

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ScotlandOnLine
Forum Newbie
Posts: 5
Joined: Sat Mar 15, 2003 4:57 pm

Newbie Question - Storing Images

Post 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 !
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post 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
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

I'll second that
ScotlandOnLine
Forum Newbie
Posts: 5
Joined: Sat Mar 15, 2003 4:57 pm

Post by ScotlandOnLine »

Which bit jim ?

the fact that dave is a bottom feeder - lol

-or don't store the images in the database directly?
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post 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:
ScotlandOnLine
Forum Newbie
Posts: 5
Joined: Sat Mar 15, 2003 4:57 pm

>>Jim

Post 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....
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post 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.
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post 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)
ScotlandOnLine
Forum Newbie
Posts: 5
Joined: Sat Mar 15, 2003 4:57 pm

Daven

Post 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!
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post 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.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post 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 :/)
Image Image
Post Reply