I have a simple user comment system. These can come with an image. Currently I am storing user uploaded images in a folder. I do not write anything to the DB for the image, very simply I name the image file with the comment id (only one image per comment allowed) and store them all in one folder:
move_uploaded_file($upload_pic,'images/'.mysql_insert_id().'.jpg');
//please note I am also upgrading to mysqli
then while retrieving comment data from database I am checking with file_exists() and potentially displaying the full image with the comment.
if (file_exists('images/'.$fid.'.jpg')) echo '<img width="576" src="images/'.$fid.'.jpg">';
Now I want to change it to be able to store and show more images per post (still limited to 4) and have also thumbnails.
For such simple system is it faster to store all file references in a separate table in the database or enhance the file naming related to comment ID ?
XXXXXX-Y.jpg
and retrieving with glob("images/".round(XXXXXX/1000)."/XXXXXX-*.jpg")
, having max 1000 files in one folder. (currently there's 6.5k images in 105k posts over last 10yrs) To me the file naming seems more simple as I do not need to store any additional information related to the image, but on the other hand it seems majority of people uses database. And would like to focus on the way with better performance.
In my experience from working on a real estate website its quite expensive to check for file exists.
So if you have a table with file names in database that will much faster to display. You will not be checking explicitly for file_exists for all the four files every time, you will just display the image tag as many times as you have images for that comment in db. Like if you have 1 image for a comment you will not need to check for the other 3, you only found 1 from db, so you just display it at once.
For the naming, keep things simple like using this:
3673-1-originalfilename.jpg
3673-2-originalfilename.jpg
3673-3-originalfilename.jpg
which follow the following pattern
"commentid"-"count"-"original-filename"."extension"
Needless to say but you can store more data about the images in the database, if you have to like the thumbnail file name as well or file size etc, and you can store any number of images for a comment as you want.
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.