2

I'm trying to make an image uploader, but it always give me this error

Call to a member function getClientOriginalName() on a non-object here is my code controller code

public function postSubtitle()
{
    //$video = Video::where('user_id', '=', Auth::id())->find(Input::all('id'));

    var_dump(Input::all());
    $file= Input::file('name');
    echo $file->getClientOriginalExtension();   
}

and here is the upload form

{{ form_open({'url': 'video/subtitle', 'files': 'true'}) }}

                    {{ form_file('name', {class: 'form-control'} ) }}
                    {{ form_submit(trans('main.edit'), {class: 'btn btn-lg btn-success btn-block'}) }}
                    {{form_close()}}

what's wrong with my code?

Jensej
  • 1,067
  • 6
  • 16
  • 32

2 Answers2

6

If you want to handle file uploads, your form must have enctype="multipart/form-data". (You might have that, but we don't know how your function form_open works.)

If you have that but still get the same error it should mean that you haven't selected any file. If you don't upload a file, Input::file('name') will be null instead of an object.

So what you should do is to first check if it's not null, and then continue to handle your file.

$file = Input::file('name');
if ($file !== null) {
    echo $file->getClientOriginalExtension();  
}
Community
  • 1
  • 1
Marwelln
  • 25,688
  • 19
  • 82
  • 110
1

I thing should be remember. Pass only file path e.g name of input type only ,not pass any plain file name.this error occur when we pass wrong format, so i suggest you first check carefully,

Solution here :

$image = $request->file('edit_image');
  $destinationPath = public_path('/images');
  $new_name = rand().'.'.$image->getClientOriginalExtension(); 
  $image->move($destinationPath, $new_name);
  PlayersModel::where('id', $edit_user_id)->update($players_Obj);
Aslam Khan
  • 81
  • 2