Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

Uploaded files renaming - using uuid instead of the file->hashName()

Hello,

I noticed that Laravel's hashName() uses the random string method with length of 40. But would that be better to use uuid instead?

i.e.:

// in controller
$file => $request->file;
$uuid = Str::uuid();
$extension = $file->extension();
$hashed_name = $uuid . "." . $extension; // or even not use extension at all, and store it in the DB
0 likes
5 replies
Snapey's avatar

40 character string has way more entropy than a uuid. What problem are you trying to solve?

1 like
Ligonsker's avatar

@martinbean @snapey no collisions I was just thinking maybe it's more "standardized"? But I guess not

If I do use random string and have a collision - the putFileAs would return false, or will it override an existing file? I know it would never happen in my app, I am just curious

Snapey's avatar

@Ligonsker probably just replace the original.

If you were concerned you could put the files in a folder named after the id of the related model which avoids having large numbers of files in a single folder

martinbean's avatar

@Ligonsker I’m sure Laravel wouldn’t have picked the random approach if it didn’t have great enough entropy to avoid duplicates/overwrites.

Please or to participate in this conversation.