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 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳