自定义文件名,同时保留扩展名 [英] Custom file name while keeping extension
问题描述
遵循这部分文档:
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.
如果我这样做:
$request->file('avatar')->storeAs('avatars', 'custom_file_name');
文件将另存为 custom_file_name
(没有扩展名),而不是 custom_file_name.png
).
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.
UploadedFile
类还包含用于访问文件的标准路径及其扩展名的方法.extension
方法将尝试根据其内容猜测文件的扩展名.
The
UploadedFile
class also contains methods for accessing the file's fully-qualified path and its extension. Theextension
method will attempt to guess the file's extension based on its contents.
https://laravel.com/docs/5.4/requests#files
这篇关于自定义文件名,同时保留扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!