簡體   English   中英

在數組上調用成員函數getPathName()

[英]Call to a member function getPathName() on array

我正在將多文件數組上傳到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)

$request->files將返回所有文件的多維數組,而$request->file('files')將僅返回與files輸入有關的files的數組。

這對我有用:

服務器端:

$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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM