I usually would use a unique identifier of that user to name the parent directory of all of their uploads. You could even then inside their folder, make unique folders for each event if they may have multiple uploads per event. This in my experience makes it easier to delete all user uploads, specific event uploads, etc.
Does anyone put all of the images into one folder?
I was going to put every event image a user uploads into one folder. If I have thousands of events this could be a lot of images in one folder. Is this a bad practice? What is the best way to deal with a folder structure for images?
Hi @chrisgrim as a rule of thumb I usually do not put all images in the same folder as performance of file system can degrade. Usually I try not to have more than 1,000 files in the same folder.
Quick reference I could find googling it: https://stackoverflow.com/questions/466521/how-many-files-can-i-put-in-a-directory
In that link, the best answer suggests that performance of basic commands such as ls degrade starting on more than 10,000 files in a folder. There are other answers where the users states they hold huge amount of files on the same folder (one say they had 8 million files in a folder), but they also say they notice degraded performance.
One approach I use is to extract the folder name from the image name. If the image has an auto-increment ID, you can generate its folder name from the thousands places. For example for images numerated from 1 to 999 they would go to the 0000 folder, from 1,000 to 1,999 they would go on the 0001 folder and so on.
When the images are not related to an auto-increment ID, such as user uploads, I usually take the first 4 characters from the image filename md5 hash and use this as the folder name to place the image. This approach is similar to how Laravel's cache file drive store entries on the filesystem , although in the file driver Laravel uses the first two characters from a sha1 hash generated from the entry's key.
reference: https://github.com/laravel/framework/blob/7.x/src/Illuminate/Cache/FileStore.php#L232-L243
Please or to participate in this conversation.