how to create a database that will add an image?

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
rjuy
Forum Newbie
Posts: 15
Joined: Thu Apr 17, 2008 4:56 am

how to create a database that will add an image?

Post by rjuy »

can u pls help me, i kept on searching on how to add an image into the database but i haven't found a image data type.......

so how can i add an image in my database if there is no image datatype,

pls help me guys..........

thanks..............
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: how to create a database that will add an image?

Post by aceconcepts »

What you would do is add the image name (including extension) to the database and upload the actual image file to a directory on your server.

When you need to display the image simply use the directory path of where the images is stored and reference the name of the file from the database.
m4rv5
Forum Newbie
Posts: 13
Joined: Fri Apr 11, 2008 12:49 am

Re: how to create a database that will add an image?

Post by m4rv5 »

google is your best friend :)
http://www.google.com.sg/search?q=mysql ... =firefox-a

This article basically covers also what you need. But the difference from what ace suggested is that this stores the binary file itself onto the database .
http://www.phpriot.com/articles/images-in-mysql
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: how to create a database that will add an image?

Post by onion2k »

Images are binary data ... so use a BLOB (binary large object) data type.

It's not really a good idea though. Files shouldn't be stored in the database except under very specific and unusual circumstances. If they're in the database you need to open a connection, stream the binary data out to a script which holds it in memory, then stream that out to the user. It's really inefficient compared to just storing the filename and having your script fetch the file from the file system instead.
mabus
Forum Newbie
Posts: 17
Joined: Wed Apr 16, 2008 11:52 pm

Re: how to create a database that will add an image?

Post by mabus »

I would strongly suggest saving the filename of the image to the database, instead of using blob. It will cost you less overhead on your db.

However, there are things that you will need to consider with the filenames. Here are among some of them..

1. You need a place to store the files, and such directory should be manageable enought. I don't think it is a good idea to include the whole path in your database entry, but you should manipulate this from your php scripts.

2. Filenames should be unique to avod overwritting. One solution is to use UUID, or encrypt the filename, or make string manipulations to the filename.

There will be a lot more, but you shall tackle them when you write your app.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: how to create a database that will add an image?

Post by aceconcepts »

To prevent overwriting a file in your directory you could always use http://uk3.php.net/function.file-exists
rjuy
Forum Newbie
Posts: 15
Joined: Thu Apr 17, 2008 4:56 am

Re: how to create a database that will add an image?

Post by rjuy »

thanks you guys

-rjuy
Post Reply