“Storage::putFile”的 Laravel 5.4 用法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45419672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.4 usage of "Storage::putFile"
提问by Mydisname
I am working with Laravel 5.4 and want to upload a file (let's call it "lorem.ext") to the storage directory (storage/app/) "path/to/file" with a unique file name.
我正在使用 Laravel 5.4 并希望使用唯一的文件名将文件(我们称之为“lorem.ext”)上传到存储目录(storage/app/)“path/to/file”。
For that I want to use Storage::putFile (https://laravel.com/docs/5.4/filesystem#storing-files) which not only stores my file, but also automatically creates a unique file name.
为此,我想使用 Storage::putFile ( https://laravel.com/docs/5.4/filesystem#storing-files),它不仅存储我的文件,还自动创建一个唯一的文件名。
The documentation says to use such:
文档说使用这样的:
Storage::putFile('uploadedFile', new File('/path/to/file'));
Using this, I will get the error
使用这个,我会得到错误
FileNotFoundException in File.php line 37: The file "/path/to/file" does not exist
My further thoughts:
我的进一步想法:
I honestly do not know exactly what the signature means and never found a working example from putFile in the web. In the documentation it is not described and looking closer (https://laravel.com/api/5.4/Illuminate/Filesystem/FilesystemAdapter.html#method_putFile) there is no information, as well.
老实说,我不知道签名的确切含义,也从未在网络上从 putFile 中找到工作示例。在文档中它没有被描述并且仔细观察(https://laravel.com/api/5.4/Illuminate/Filesystem/FilesystemAdapter.html#method_putFile)也没有信息。
What I think it means:
我认为的意思是:
The first parameter "uploadedFile" (or as https://laravel.com/docs/5.4/filesystem#storing-filescalls it: "photos") will automatically get the file via the ID from the form in the view:
第一个参数“uploadedFile”(或如https://laravel.com/docs/5.4/filesystem#storing-files所称的:“photos”)将通过视图中表单的 ID 自动获取文件:
<input type="file" id="uploadedFile" name="uploadedFile">
and there is no need anymore to load it via
并且不再需要通过加载它
request()->file('uploadedFile')
The second parameter "new File('/path/to/file')" (or as https://laravel.com/docs/5.4/filesystem#storing-filescalls it: "new File('/path/to/photo')") will specify the path in the targetstorage directory on the server without the file name:
第二个参数“new File('/path/to/file')”(或如https://laravel.com/docs/5.4/filesystem#storing-files所称:“new File('/path/to/ photo')") 将指定服务器上目标存储目录中的路径,不带文件名:
.../storage/app/path/to/file/formerLoremNowUniqueFileName.ext
So complete example where I can upload my lorem.ext and it will get stored on .../storage/app/path/to/file/formerLoremNowUniqueFileName.ext (which does not work):
如此完整的示例,我可以上传我的 lorem.ext 并将其存储在 .../storage/app/path/to/file/formerLoremNowUniqueFileName.ext (不起作用):
View:
看法:
<form method="POST" action="URL/TO/STORE" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="file" id="uploadedFile" name="uploadedFile">
<button type="submit" class="btn btn-primary">Upload</button>
</form>
Controller:
控制器:
public function store() {
return Storage::putFile('uploadedFile', new File('/path/to/file'));
}
Can anybody
任何人都可以
- describe the signature of "putFile" so that I get it? ;-)
- tell me why my example is wrong and why I do get this FileNotFoundException?
- 描述“putFile”的签名以便我得到它?;-)
- 告诉我为什么我的例子是错误的,为什么我会得到这个 FileNotFoundException?
Thank you!
谢谢!
回答by Ken
First argument passed to putFile
is the new file location. Example: /path/to/new/file/
传递给的第一个参数putFile
是新文件位置。例子:/path/to/new/file/
The second argument is a File
or UploadedFile
instance. This can come from request()->file('uploadedFile')
.
第二个参数是一个File
或UploadedFile
实例。这可以来自request()->file('uploadedFile')
.
Thus your controller should read
因此你的控制器应该阅读
public function store() {
return Storage::putFile(
storage_path('uploads'),
request()->file('uploadedFile')
);
}
A generated hash will be used for the file name.
生成的哈希将用于文件名。