在空Laravel上调用成员函数getClientOriginalName() [英] Call to a member function getClientOriginalName() on null laravel

查看:896
本文介绍了在空Laravel上调用成员函数getClientOriginalName()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试上传pdf文档文件时,我一直遇到此错误,有人知道如何解决此问题吗?

I keep on getting this error when I try to upload a pdf document file, does anybody have any idea how to solve this?

我曾尝试寻找类似的问题,但仍然无法解决(例如:使用Laravel 5上传pdf文件)

I had tried looking for similar problems but still can't solve it(eg: Upload pdf file using Laravel 5)

我尝试执行dd()来查看文件是否已上传,并且确实显示了文件名,但是错误显示Call to a member function getClientOriginalName() on null,所以现在我对现在该做什么感到困惑.

I tried doing dd() to see if the file have been uploaded and it did show the file name but the error said Call to a member function getClientOriginalName() on null, so now I'm kind of confused on what to do now.

这是我的代码,在此先感谢您的帮助.

Here are my codes, thanks in advance for helping.

控制器:

 class CreateController extends Controller
{
    public function create(){

    return view('create');
}
public function store(Request $request){

  $uniqueFileName = uniqid() . $request->get('upload_file')->getClientOriginalName() . '.' . $request->get('upload_file')->getClientOriginalExtension();

   $request->get('upload_file')->move(public_path('files') . $uniqueFileName);
//dd($request);
   return redirect()->back()->with('success', 'File uploaded successfully.');
}

create.blade.php

create.blade.php

   <form  enctype="multipart/form-data" class="form-horizontal" method="post" action="{{ url('/user')}}">

             {{  csrf_field()  }}
          <div class="form-group">
     <label for="upload_file" class="control-label col-sm-3">Upload File</label>
     <div class="col-sm-9">
          <input class="form-control" type="file" name="upload_file" id="upload_file">
     </div>
</div>


 <div class="form-group">
            <div class="col-md-6-offset-2">
              <input type="submit" class="btn btn-primary" value="Save">
            </div>
          </div>
</form>

路线:

Route::get('/user/create','CreateController@create');
Route::post('/user','CreateController@store');

推荐答案

您需要使用file()函数才能访问您上传的文件

You need to use the file() function to have access to your uploaded file

$request->file('upload_file')

这篇关于在空Laravel上调用成员函数getClientOriginalName()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆