【问题标题】:Laravel 5.4: hasFile() no longer worksLaravel 5.4:hasFile() 不再有效
【发布时间】:2019-01-10 10:49:59
【问题描述】:

这在 Laravel 5.3 下完美运行。

我刚刚升级到 5.4,无法再使用hasFile() 方法上传文件。检查被忽略

public function addPhotos(Request $request)
{
    // dd($request) here shows a file in the files:FileBag

    if ($request->hasFile('photo'))
    {
        // dd($request) here never prints,
        this block is passed over as if the file doesn't exist
    }
}

有什么想法吗?


更新

当我转储文件时,这是我得到的,所以你可以看到有些东西正在上传......

dd($request->file('photo'));

返回...

array:1 [▼
  0 => UploadedFile {#504 ▼
    -test: false
    -originalName: "test.jpg"
    -mimeType: "application/octet-stream"
    -size: 0
    -error: 1
    #hashName: null
    path: ""
    filename: ""
    basename: ""
    pathname: ""
    extension: ""
    realPath: "/Users/tim/Sites/myapp/public"
    aTime: 1969-12-31 19:00:00
    mTime: 1969-12-31 19:00:00
    cTime: 1969-12-31 19:00:00
    inode: false
    size: false
    perms: 00
    owner: false
    group: false
    type: false
    writable: false
    readable: false
    executable: false
    file: false
    dir: false
    link: false
  }
]

【问题讨论】:

  • Request的命名空间是什么?
  • @alaric use Illuminate\Http\Request;
  • 嗯,应该仍然存在,我相信你已经看到了。它已在 5.4 中移至 trait,但函数的签名和实现是相同的。我想如果该方法无论如何都不存在,您将收到一个致命错误。您是否尝试过临时编辑供应商源以确定特征方法中发生的情况?作为参考,你会寻找github.com/laravel/framework/blob/5.4/src/Illuminate/Http/…

标签: laravel file-upload


【解决方案1】:

尝试使用以下方法而不是 $request->hasFile

$image = $request->file('picture');
if ($image->isValid()) //return true if the file has been uploaded with HTTP and no error occurred
{ 
$imageName = time().'.'.$image->getClientOriginalExtension();//assign name of image with time prefix and image extension
$destinationPath = public_path('/images');//assign destination path in public folder
$image->move($destinationPath, $imageName);
  $user->image=$imageName;
  if($user->save())
  {
   return Response::json(['status' => 1, 'message' => 'Image updated','imageName'=>$imageName]);
   }
   else
   {
    return Response::json(['status' => 0, 'message' => 'Image not updated']);
    }
}        

【讨论】:

  • 返回null
  • 请在 laravel 表单模型中启用 'files'=>'true'
  • 是的。正如我在帖子中所说,这是在 Laravel 5.3 下运行的;在升级到 5.4 之前,我能够上传 1 个或多个文件。
  • 这不是答案,但确实为我指明了正确的方向。 +1。
【解决方案2】:

根据文档,https://laravel.com/docs/5.4/requests 它仍然存在。你确定你做得对吗?

【讨论】:

    【解决方案3】:

    原来我正在使用无效的 jpeg 进行测试(这到底是怎么回事?)所以我必须创建一个可以处理这种情况的方法。

    另外,我的输入键是一个允许一个或多个文件的数组,所以我也必须处理它......

    public function uploadIsValid($request)
    {
        foreach ($request->file('photo') as $file) {
            if (!$file->isValid()) {
                return false;
            }
        }
    
        return true;
    }
    

    然后在我的addPhotos() 方法中

    if ($this->uploadIsValid($request)) {
        // process uploaded files here...
    }
    

    【讨论】:

      【解决方案4】:

      尝试使用:

      use Illuminate\http\UploadedFile;
      

      【讨论】:

        猜你喜欢
        • 2017-06-01
        • 1970-01-01
        • 2017-04-02
        • 1970-01-01
        • 2015-03-27
        • 2019-11-21
        • 1970-01-01
        • 1970-01-01
        • 2018-04-22
        相关资源
        最近更新 更多