Skip to content
Advertisement

How can get the name of a image stored in /storage/public if was dynamic name

My problem is that i upload a image on the website in laravel but the database save the path of my local storage with a strange name and extension: Data of my database

The picture was uploaded fine in Storage/app/public but with another name (Like a hash code): Name of the picture

Now, how can get this name in /Storage/Public with a query on a database, or how can upload the image with the original name or by ID?

Code: Form upload:

JavaScript

In controller:

JavaScript

View where I need to show the picture:

JavaScript

Advertisement

Answer

As I can see, you don’t save the path with the file extension.

When you use $request->file('icono')->store('public'); you generate a temporal file and the file extension is determined by Laravel with the MIME type.

What’s about:

JavaScript

For the filename, I’ve used Str::uuid function to generate names with a different id. For store files, I’ve used Storage Laravel facade, take a quick look here: https://laravel.com/docs/8.x/filesystem#specifying-a-file-name

What do you think about these changes?

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement