/    Sign up×
Community /Pin to ProfileBookmark

Should I use UUIDs to identify data in my database

I’m working on a little project, and in order to store users images related to an article, I decided to create a folder on the sever with the path “uploads/images” and then, inside this images folder, every time a user uploads images tied to a product, a new folder is created inside image with his name and a generated ID number, thus resulting in a path like “uploads/images/jane___doe123456789”. Then I will only have to store the folder path in the database and I wouldn’t have to rename every single image posted by a user. However I wondered if I should be using UUIDs to generate the folder names or another way?

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 16.2021 — For some reason I feel like using a distinct user ID seems better than splashing actual user names all over the file system. :)

As far as a separate folder for each product (or whatever?), one concern that pops to mind is making sure whatever you think the largest number of products are you might ever have to support does not exceed whatever the server's operating system restrictions are for the number of folders under the same parent folder. (I have no idea what those numbers might typically be, and maybe is so high that you don't need to worry, but still might be worth a bit of googling to be sure.)
Copy linkTweet thisAlerts:
@ginerjmSep 16.2021 — I would not store the path in the table. Save it in your app and use it to make your queries instead. That way if you ever have to re-locate your db or rename some folder you don't have to edit every single record.

And why a separate folder for each user when you have a unique id for the image?
Copy linkTweet thisAlerts:
@SalvignolauthorSep 16.2021 — @NogDog#1637125 That is indeed something i really didn't think about. So should I create a folder for each user but ditch every images of any product they might post in that same folder rather than creating a folder for each product?
Copy linkTweet thisAlerts:
@SalvignolauthorSep 16.2021 — @ginerjm#1637126 I'm a bit lost when you say save it in the app 😅. Do you mean saving all the images in that "image" folder and just fetching them by name?
Copy linkTweet thisAlerts:
@NogDogSep 17.2021 — > @Salvignol#1637129 So should I create a folder for each user

That could run into the same problem: could you have so many users submit photos such that you'd reach a folder limit?

The last time I dealt with anything vaguely like this (several years ago), I think I just dumped all the images in the same directory, used something like the PHP uniqid() function to create a file name, then saved that in a DB table that included info about the image along with a foreign key to the user.
Copy linkTweet thisAlerts:
@ginerjmSep 17.2021 — I was saying that you should save the path name someplace that your app can just go and read when it needs it - like at startup. Then when you need to store or retrieve something from that spot you know what the path is. Storing the path in the table on each and every record is just not right.
Copy linkTweet thisAlerts:
@SalvignolauthorSep 17.2021 — @NogDog#1637133 Ok, that is pretty clear. And it seems better. So I basically create a new table with columns such as Id, userID(FK), uniqID, timestamp etc... and just use the uniqID to get the images related to a product the user posted.
Copy linkTweet thisAlerts:
@NogDogSep 17.2021 — @Salvignol#1637136 Yeah, I think that's the more robust approach, especially if the intent/hope is to have lots of users/products/images/whatever -- for some undefined value of "lots". :) If, on the other hand, you expect those things to be limited in number (e.g. only "editor" users can post images or something), then maybe it's overkill. Plus, I'm biased, as my current job of several years has me down in the database weeds at some point pretty much every day.
Copy linkTweet thisAlerts:
@SalvignolauthorSep 18.2021 — @NogDog#1637160 Thanks a lot your help has been invaluable
×

Success!

Help @Salvignol spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...