【问题标题】:Uploading images with laravel 5.2使用 laravel 5.2 上传图片
【发布时间】:2016-08-16 03:51:14
【问题描述】:

我正在尝试为我的 laravel 项目创建一个简单的上传图片功能,但是我不断收到此错误:

FatalThrowableError in UploadController.php line 23:
Fatal error: Call to a member function hasFile() on null

这是我的控制器:

  public function uploadImg(Request $request){
    $input = $request->input();
    if($input->hasFile('file')){
      echo 'Uploading';
      $file = $input->file('file');
      $file->move('uploads', $file->getClientOriginalName());
      echo 'Uploaded';
    }
  }

这是我的表格:

<form action="/admin/media/uploadImg" method="post" enctype="multipart/form-data">
    <label>Select image to upload:</label>
    <input type="file" name="file" id="file">
    <input type="submit" value="Upload" name="submit">
    <input type="hidden" value="{{ csrf_token() }}" name="_token">
  </form>

【问题讨论】:

    标签: php html forms laravel laravel-5.2


    【解决方案1】:

    hasFile() 方法仅适用于请求对象,而不适用于输入数组。试试这个:

    if($request->hasFile('file')){
    

    见:https://laravel.com/docs/5.2/requests#files

    您还需要更改此行:

    $file = $input->file('file');
    

    收件人:

    $file = $request->file('file');
    

    【讨论】:

      猜你喜欢
      • 2016-09-01
      • 1970-01-01
      • 2016-08-18
      • 2018-04-17
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      相关资源
      最近更新 更多