douci1541 2017-10-23 08:52
浏览 178
已采纳

在null laravel上调用成员函数getClientOriginalName()

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

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

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.

Controller:

 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

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

Route::get('/user/create','CreateController@create');
Route::post('/user','CreateController@store');
  • 写回答

3条回答 默认 最新

  • duan6161 2017-10-23 08:54
    关注

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

    $request->file('upload_file')

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分