我正在将多文件数组上传到Laravel中的服务器。
使用$request->file('files')
登录文件时,我得到:
[2018-11-12 16:10:03] local.DEBUG: array (
0 =>
Illuminate\Http\UploadedFile::__set_state(array(
'test' => false,
'originalName' => 'test-pdf.pdf',
'mimeType' => 'application/pdf',
'error' => 0,
'hashName' => NULL,
)),
1 =>
Illuminate\Http\UploadedFile::__set_state(array(
'test' => false,
'originalName' => 'test-pdf.pdf',
'mimeType' => 'application/pdf',
'error' => 0,
'hashName' => NULL,
)),
2 =>
Illuminate\Http\UploadedFile::__set_state(array(
'test' => false,
'originalName' => 'test-pdf.pdf',
'mimeType' => 'application/pdf',
'error' => 0,
'hashName' => NULL,
)),
)
我想访问数组中的每个文件并像这样获取路径名:
$files = $request->files;
foreach ($files as $key => $file) {
Log::debug($file->getPathName());
}
但是这会引发以下错误:
local.ERROR: Call to a member function getPathName() on array {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to a member function getPathName() on array at /home/vagrant/Projects/business-risk-portal/app/Http/Controllers/FileController.php:68)
如何访问每个上载文件的文件路径?
更新 如果我尝试这样做:
$files = $request->files;
foreach ($files as $key => $file) {
$temp_path = $request->file('tmp.' . $key);
Log::debug($temp_path->getPathName());
}
我得到:
Call to a member function getPathName() on null {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to a member function getPathName() on null at /home/vagrant/Projects/business-risk-portal/app/Http/Controllers/FileController.php:69)
答案 0 :(得分:1)
$request->files
将返回所有文件的多维数组,而$request->file('files')
将仅返回与files
输入有关的文件的数组。
答案 1 :(得分:0)
这对我有用:
服务器端:
$files = $request->file('myfiles');
foreach($files as $file) {
$target = $file->move(storage_path('files'), $file->getClientOriginalName());
$path = $target->getPath() . DIRECTORY_SEPARATOR . $target->getFileName();
}
查看:
<form method="post" action="/myfiles" enctype="multipart/form-data">
{{csrf_field()}}
<input type="file" name="myfiles[]" class="form-control" multiple />
<button type="submit" class="btn btn-success">Save</button>
</form>