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.
Here comes the question
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 ?
- If database - how to name the files ? using uniqid ? Simple mapping table id, imgname.
- If file naming I was thinking of
XXXXXX-Y.jpg
and retrieving withglob("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.