Reputation: 3889
A quick question on best practice for handling images in a PHP/MySQL CMS. Each item will have a varying number of associated images which will be stored in folders referenced by the items ID. Validation is done on upload so all files should be valid. My question is whether I should also store the ID and filename(s) in a database table and pull the source data from the DB or is it OK to simply iterate through the folder and insert the source filenames directly?
I hope that makes sense. Thanks in advance for any advice.
Upvotes: 0
Views: 941
Reputation: 9582
It largely depends on how many points of failure you want and the speed of the response time.
If you store it on the filesystem:
However, if you store it as a binary blob in a database:
In both cases step 3 (the database finds the row) can be just as fast with our WITHOUT the blob column as long as proper indices/keys are set up. MySQL will move the pointer to the exact indexed position internally - it won't actually go through every byte until it finds the right one (that's the whole point of an index). This is just as time consuming as PHP reading the file manually. However, I currently do not have supporting performance data to support this.
Now let me talk about points of failiure:
I have always stored binary data as a blob in the database for the purpose of portability, flexibility, and minimizing failure or corruption.
If you choose to store the file in the database as a blob, consider the different blob storage requirements: http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html#id656744
Lastly, just food for thought: I have experience working with Adobe's Day CQ which is an up-and-coming enterprise-level content management system. It is primarily written in Java, but what is important to note is the data architecture. It uses a JCR (Java Content Repository) which more or less acts like a multidimensional MySQL database (kinda cool?). All image data in its DAM (digital asset manager) is stored as a node within the JCR.
Upvotes: 1