Laravel File Upload: Duplicate Filenames - 3 Ways To Solve

preview_player
Показать описание
If different users of your application upload the files with the same filename, but you do want to store the original filenames and not hash them, there are a few ways to solve it.

Related links:

- - - - -
Support the channel by checking out our products:
Рекомендации по теме
Комментарии
Автор

Generally, I resolve this by completely ignoring the original file name when storing the media, and just ensure the path is unique within my application. So something like "/avatars/1", ".avatars/2", etc. Because I can derive that path from the primary key or the corresponding user, I often won't store it anywhere. The original filename and MIME type will however get stored in the database. Then, when the avatar needs to be displayed, I'll funnel the request through my own controller action that fetches the content from storage, but also sends the correct headers to set the original filename, and MIME type.

It's worth noting, that if you store media with the original file extension, and theres any possible way for the user to upload a PHP file then request it directly (e.g. via the public disk in Laravel storage). You've got a serious security vulnerability. If you're going to allow uploaded files to be requested directly, you need to sure the PHP engine is turned off for that folder. This is the main reason why I store all of my media privately (without extension), and funnel all requests through a controller where I can make sure the media content is never executed.

JimOHalloran
Автор

I personally do not understand what is the point of preserving original file name - unless you're doing some hosting thing and the file name really matters for the users, for simple things like avatars Laravel came with hashName() out of the box.

Ofeaudacity
Автор

I am usally using uuids or random strings instead of names, I only keep extensions.

wizjoner
Автор

As usual great video.

I use uuid for naming the file while storing.

Sometimes I also prefer using the combination of user_id and name of the file.

adityakadam
Автор

Doesn't PHP have a function to assign random names to files at upload?

Flankymanga
Автор

I usually just grab the unix epoch of the moment the file is uploaded, and append that with a bunch of random strings and then finally append that with the user ID

never had issues with it

thanos
Автор

Is there a way to validate the image name is unique with validation rule?
Like $request->validate([ ''image.name'' => "required|image|unique:images, originalname"])

juniordev
Автор

Does the Spatie Media Library package work with things like AWS S3? Basically, how do you think it would perform at scale?

Recoil
Автор

What about adding current time to filename without changing original name of the file? like now(). "some special characters like - to parse later on".$filename.

yasinsefakirman
Автор

What if I just hash the original file name?

guilhermemoraes
Автор

I am always rename file with 12 length unique string and if it profile image that create folder for profile. Mainly untill now client never want same name file upload.

bhaviktrambadiya
Автор

What's the difference between storing images in public vs storage folder and then linking. Which is better way and why?

abdulrehmandar
Автор

This is great, i am gonna use from now laravel spatie

Fosterushka
Автор

I usually do something like I doubt it's possible for that to be duplicated. Might be wrong though. I'll look to use the spatie media library for the additional functrionalitioes now though. Thanks for the recommendation.

olusolaojewunmi
Автор

Actually I don't know why we have to make all these steps and get the extension then hashing the name then append the extension etc, but when I started with laravel i used to use one single line

, $request->file('avatar')); // unique folder for every user with unique file name (Auto hashed)

and Laravel does the rest, am I missing something ?

waelmoh
Автор

In this case (one avatar per user) no need to take risks with a user provided filename. Instead name the file avatar-123.extension where 123 is $user->id and extension is limited to a whiltelist like ['jpg', 'png', 'webp']

paulfontaine
Автор

I previously used the renaming method like avatar-{user_id}.png

but thanks for the Spatie one. Definitely will use it from now on

doctorneuron
Автор

I'm very much interested about knowing, how do you guys delete old files?

shakiltech
Автор

all 3 methods are great but you have to make sure that you also check for old avatar. For example if in first time they upload jpg file as avatar, then 2nd time they upload png file, then that user will have 2 file. But maybe because it's only avatar in example case, it won't have any problem, but in bigger case it will left many orphans files in storage server.

RizkheyG
Автор

My first and only time where I had to deal with users files I did it by creating folders for each user on creation.

medilies