代码之家  ›  专栏  ›  技术社区  ›  Marcus Christiansen

对数组的成员函数getPathName()的调用

  •  0
  • Marcus Christiansen  · 技术社区  · 6 年前

    当我使用 $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)
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Devon Bessemer    6 年前

    $request->files $request->file('files') 将只返回与 files 输入。

        2
  •  0
  •   adam    6 年前

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