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?
You can get extension of uploaded file with:
$extension = $request->photo->extension();
And apply it manually to the stored file name.
The
UploadedFileclass also contains methods for accessing the file's fully-qualified path and its extension. Theextensionmethod will attempt to guess the file's extension based on its contents.
https://laravel.com/docs/5.4/requests#files
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With