Reputation: 174957
I am implementing an image upload system in PHP, The following are required:
For that, I have 2 approaches in mind:
Each category will have its own folder, and PHP will detect categories via those folders.
Each image in the database will have a catID (or multiple catIDs), and PHP will query the database to get the images
Which do you think is better? Or is there a third, completely different, approach that I'm missing?
Just a note, I don't need code, I can implement that myself, I'm looking to find what to implement.
Would love to hear from you.
Upvotes: 4
Views: 627
Reputation: 174957
I eventually went with the best answer of this question: Effeciently storing user uploaded images on the file system.
It works like a charm. Thanks for all of the answers!
Upvotes: 0
Reputation: 86
I believe that the second option is better, a DB is giving you much more flexibility, and I think better performance then file system, if you set the right indexes.
In the filesystem approach you are limited to only 1 category per image, when in the DB you can set multiple categories on an image.
The con that Db is more messy, sorry I can't find a reason way in the db it will be more messy, maybe you mean that the files are not organized on the file system, but you still need to organize the files on the file system and divide them to multiple folders for better performance, and if you want to get all the images that have been uploaded you query the db for all of them, which will be much faster then ls on all the categories folders.
In organize the files in the file system when using the DB approach I mean that you need to divide them to several folders, actually it depends on how you predict the upload of the images will be:
The second con that need to query the DB a lot is not quite true, cause you will need the same amount of queries on the file system which are far more slower.
Good luck.
PS Don't you forget to generate a new file name to any image that have been uploaded so there will be no collisions in different users uploaded same image name, or the same user.
Upvotes: 3
Reputation: 272096
Since you need a database to store comments and ratings, you should store categories in database as well. Sometime later you may also want to store image captions and description; database allows you to do that. And I would not worry about querying the database a lot.
Whether to store the image itself in database or filesystem is a separate issue which is discussed here.
Note about storing images in filesystem: do not store thousands of images in a single directory; it could cause performance issues for the OS. Instead invent a way to organize images in sub directories. You can group them by dates, filenames, randomly etc. Some conventions:
upload date: month/year
/uploaded_images
/2010/01
/2010/02
upload date: month-year
/uploaded_images
/2010-01
/2010-02
md5 hash of image name: first character
/uploaded_images
/0/
/1/
.
.
.
/e/
/f/
batches of thousands
/uploaded_images
/00001000/
/00002000/
/00003000/
Upvotes: 1
Reputation: 2469
The second solution (database) is actually a TAG/LABEL system of categorizing data. And that is the way to go, biggest examples being Gmail and Stackoverflow. Only thing you need to be careful about is how to model tags. If the tags are not normalized properly, querying from database becomes expensive.
Upvotes: 2
Reputation: 157839
Use folders only to make file storage reliable, storing certain amount of files per folder, i.e.
/b/e/beach001.jpg
as for your dilemma, it is not a question at all.
From your conditions you can say it yourself that database is the only solution.
Upvotes: 1
Reputation: 31730
I'd be inclined to go with the database approach. You list the need to query the database a lot as a con, but that's what databases are built for. As you pointed out yourself, a hierarchical structure has serious limitations when it comes to items that fall into more than one category, and while you can use native PHP functions to navigate the tree, would that really be quicker or more efficient than running SQL queries?
Naturally the actual file data needs to go somewhere, and BLOBS are problematic to put it mildly, so I'd store the actual files in the filesystem, but all the data about the images (the metadata) would be better off in a database. The added flexibility the database gives you is worth the work involved.
Upvotes: 2