如何使用 Laravel & Slim 从 post 请求中获取文件属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30964849/
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
How to get file properties from a post request using Laravel & Slim
提问by NicuVlad
I'm using Laravel + Slim to build an application.
我正在使用 Laravel + Slim 来构建一个应用程序。
I have a form with file upload and I managed successfully to get the information from text inputs:
我有一个带有文件上传的表单,我成功地从文本输入中获取了信息:
$request = $app->request;
$title = $request->post('title');
But how I can get the properties like size, name, tmp_name, etc. from a file input?
但是如何从文件输入中获取诸如大小、名称、tmp_name 等属性?
I found thisquestion and tried like that but I get this error:
我发现了这个问题并尝试过,但我收到了这个错误:
Fatal error: Call to undefined method Slim\Http\Request::file()
致命错误:调用未定义的方法 Slim\Http\Request::file()
回答by Safoor Safdar
There is couple of method you can apply while you uploading some file(s) using laravel's. Here is the couple of them and most important to, in my thought.
当您使用 laravel 上传一些文件时,您可以应用几种方法。在我看来,这是其中最重要的几个。
$request->hasFile('file_name')
hasFile
method to check user upload file or not.
hasFile
检查用户上传文件与否的方法。
$request->file('file_name')->isValid()
isValid
method take care to check file have no error.
isValid
方法注意检查文件有没有错误。
After these checks, you have to get file properties and move to desired location. For these purposes.
在这些检查之后,您必须获取文件属性并移动到所需位置。为了这些目的。
$document = $request->file('file_name');
Get the document information object. So after that you can use further helper function provided my laravel FileUploader.
获取文档信息对象。所以在那之后你可以使用我的 laravel FileUploader 提供的进一步帮助函数。
$document->getRealPath();
$document->getClientOriginalName();
$document->getClientOriginalExtension();
$document->getSize();
$document->getMimeType();
and finally to move the uploaded file
最后移动上传的文件
$document->move($destinationPath);
$document->move($destinationPath, $fileName);
update
更新
However if you using slim to upload file, you have to use native php methods like globals Files
array to get file and move_uploaded_file to move file and like so.
但是,如果您使用 slim 上传文件,则必须使用本机 php 方法,例如 globalsFiles
数组来获取文件和 move_uploaded_file 来移动文件等等。
If you still have any question related to upload a file or file validation let me know.
如果您对上传文件或文件验证仍有任何疑问,请告诉我。