Web Hosting Talk







View Full Version : images in mysql database


djacks89
02-25-2006, 07:45 PM
What's the best way to upload or insert images into a mysql database and then retrieve and present them on a webpage. Can it be done with microsoft access or dreamweaver mx? What is the best method. I've already created the table with the medium blob column. I just want to know the most efficient way of inputting the actual images and retrieving them on a webpage.

01globalnet
02-25-2006, 08:19 PM
Storing the images in blob fields will make the database quite heavy and it will be more difficult making regular backups, especially if you are on a shared host...

The best way is to store only the filename (or full path) of the image.

djacks89
02-25-2006, 08:51 PM
Sounds good. What's the best way to implement this plan?

maxymizer
02-25-2006, 10:02 PM
Save images in a certain directory, but generate different image names upon moving them to upload directory so you will avoid filename collisions.
Store original name, filesize, mime-type and image location to the database.
After that, use PHP for output.
www.php.net/readfile should give you some idea.

Alternatively, you could just upload images to certain upload directory and store the path to the image.

SiliconWolf
02-26-2006, 06:02 AM
Alternatively, you could just upload images to certain upload directory and store the path to the image.

Been doing it that way for years. Far easier than anything else, IMO.

For display on the web, I've found it to be best to store the path as one relative to the root of your website. So if you're saving the file "image.jpg" to the directory "http://www.domain.com/images/" you put "/images/image.jpg" in the database. Makes it easy to display in the browser without having to reprocess the URL. And if you want to check its dimensions before you display it, you can just add $_SERVER['DOCUMENT_ROOT'] onto the beginning of the file path when you call getimagesize().

Just remember that you have to test for the existence of an identically named file before you save the file. I usually add a _1 or _2, etc. onto the filename, so if I upload three files named "image.jpg", they end up as "image.jpg", "image_1.jpg", and "image_2.jpg".