【问题标题】:Call to a member function move() on null Laravel when file is existed文件存在时调用 null Laravel 上的成员函数 move()
【发布时间】:2021-01-03 00:55:19
【问题描述】:

我遇到了错误

在 $request->file->move('storage/'.$filename); 的 null 上调用成员函数 move();

 $application = new Application;
        if($request->file('proposal')){
            $file=$request->file('proposal');
            $filename=time().'.'.$file->getClientOriginalExtension();
            $request->file->move('storage/'. $filename);

            $application->file= $filename;
        }

【问题讨论】:

  • 你刚刚从$file = $request->file(...) 的请求中获得了文件...使用$file->move(...) 而不是$request->file->move(...),它将在请求中寻找名为file 的输入
  • 感谢您澄清这一点。我正在关注一个教程,想知道为什么我没有得到相同的输出。谢谢

标签: php laravel file-upload


【解决方案1】:

试试这个

$application = new Application;
    if($request->file('proposal')){
        $file=$request->file('proposal');
        $filename=time().'.'.$file->getClientOriginalExtension();
        $file->move('storage/'. $filename);

        $application->file= $filename;
    }

您正在将请求文件分配给 $file 验证,并将此验证用于其他操作。

【讨论】:

    【解决方案2】:

    hashName()-> 转换文件名是随机哈希 我认为这是在 laravel 中存储文件的最佳方式

    $application = new Application;
    if($request->proposal):
         request('proposal')->hashName();
         request('proposal')->store('your path');
    endif;
    

    【讨论】:

      猜你喜欢
      • 2019-11-22
      • 2020-06-05
      • 1970-01-01
      • 1970-01-01
      • 2022-10-16
      • 1970-01-01
      • 1970-01-01
      • 2018-01-14
      • 2016-09-04
      相关资源
      最近更新 更多