Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom file name while keeping extension

Following this part of the docs:

https://laravel.com/docs/5.3/filesystem#file-uploads

I'm trying to store a file (an image) and specify a custom file name, but I don't want to change the extension.

If I do:

$request->file('avatar')->storeAs('avatars', 'custom_file_name');

The file will save as custom_file_name (with no extension), rather than custom_file_name.png).

How can I specify a custom file name while keeping the original file extension?

like image 910
user6841322 Avatar asked May 07 '26 19:05

user6841322


1 Answers

You can get extension of uploaded file with:

$extension = $request->photo->extension();

And apply it manually to the stored file name.

The UploadedFile class also contains methods for accessing the file's fully-qualified path and its extension. The extension method will attempt to guess the file's extension based on its contents.

https://laravel.com/docs/5.4/requests#files

like image 186
Alexey Mezenin Avatar answered May 11 '26 04:05

Alexey Mezenin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!