Reputation: 4148
I need to store images data in a database. I'm uploading images to a server and then, of course, I need to retrieve them from the database. I know that in the database I should only store just datas leaving the actual files to the filesystem so I'm trying to think which one may be the best way to save those datas.
What's bothering me the most is choosing a naming scheme. In the database I'll just have a filename
column, where to store just the filename plus the extension. No path, so I can config it in my application. So: what shall I use for the filename? I'd like to keep a meaningful name, so it would be cool to keep the photo title, or at least a part of it. But to ensure uniqueness which one could be the best approach?
I know Codeigniter has a string helper, but I also read about using the php function uniqid. Then I can attach the id to the picture name.
What do you suggest?
Upvotes: 0
Views: 1015
Reputation: 40582
Feel free to store the entire image in your database, as long as there won't be hundreds of thousands of them (or you just like paying for expensive SCSI drives :) ). If you're unsure, the file system is fine too.
For the image's unique id, the best approach is probably to just use the DB insert id (guaranteed unique, no threading/clashing/crying). So here's a good approach, assuming you want to keep the images in the file system:
create table images (
image_id int(20) primary key auto_increment, /* assumes mysql */
file_path varchar(2500) not null, /* the actual file name and full path */
file_label varchar(255), /** user's memorable name for the file */
description varchar(2500), /** user provided description of contents */
media_type varchar(100), /** something like image/jpeg; privided by php in upload */
);
Then you'll probably want to connect image_id with some sort of categorization or tags, a user_id, etc.
EDIT
Added for comments:
You can always build your links using the real file name if you like, even if you store them as simply the image id in your file system. You just need to utilize search friendly urls to do something like this (where 1234 is the image id and /images/ redirects to your php script):
mysite.com/images/db/1234/picture_of_a_cat.jpg)
Then you just redirect /images/db/ to your php script. Alternately, if you don't want to try rewriting them, you can just use $PATH_INFO.
For example, if you have mysite.com/images/render.php/1234/picture_of_cat.jpg:
<?php
$parts = explode("/",$PATH_INFO);
$image_id = $parts[3];
$image_from_db = null; //fetch the row from your dabatase here!
header("Content-type: ".$image_from_db['media_type']);
echo file_get_contents($image_from_db['file_path']);
Good luck! ;)
Upvotes: 1
Reputation: 2701
Storing images in a database is not a good idea, instead store the links to the images in the database. For example; create a table with the following fields
You can add any other field you want
Upvotes: 0
Reputation:
Well, I prefer that you use a hash in the name of the image, as can happen in case there are two images of the same name, and will generate a future problem.
Another tip, never want to stores the contents of a file in the database, the cost is much higher than just store the path to that file.
Upvotes: 0