blastme
blastme

Reputation: 429

Call to a member function getClientOriginalName() on null laravel

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');

Upvotes: 5

Views: 68865

Answers (7)

Shah Nawaz
Shah Nawaz

Reputation: 33

  if ($request->hasFile('file')) {
        $thumbnail = $request->file('file');
        $thumbnail = $thumbnail->getClientOriginalName();
      
    }

Upvotes: 0

Vengadesh KS
Vengadesh KS

Reputation: 89

This enctype solved function call on the null problem

<form method="" action="" enctype="multipart/form-data">

Upvotes: 8

user14155359
user14155359

Reputation: 1

if($request->has('sound_file')) {
            $image = $request->file('sound_file');
            $filename = $image->getClientOriginalName();
            request()->$image->move(public_path('images/users'), $filename);
            $sounds->image = $request->file('sound_file')->getClientOriginalName();
        }

Upvotes: 0

Arditya
Arditya

Reputation: 21

use this code in your tag form enctype="multipart/form-data"

<form method="" action="" enctype="multipart/form-data">

Upvotes: 2

Hamidreza Ghanbari
Hamidreza Ghanbari

Reputation: 307

The key to solving this problem is that you must write getClientOriginalName() function in the if clause just like that:

if($request->hasFile('logoImage')){
    $logoImage = $request->file('logoImage');
    $name = $logoImage->getClientOriginalName();
}

Upvotes: 0

Minsher
Minsher

Reputation: 11

Try to put the file into a var.

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

And get the extension and name from it.

 $uniqueFileName = uniqid() . $file->getClientOriginalName() . '.' . $file->getClientOriginalExtension();

Hope it helps.

Upvotes: 1

madalinivascu
madalinivascu

Reputation: 32354

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

$request->file('upload_file')

Upvotes: 6

Related Questions