简体   繁体   中英

laravel 5.2 | upload file - Call to a member function getClientOriginalName() on null

i tried to upload profile picture pict but i got error "Call to a member function getClientOriginalName() on null"

this is my method :

  $data = $request->input('fotodosen');
    $photo = $request->file('fotodosen')->getClientOriginalName();
    $destination = base_path() . '/public/uploads';
    $request->file('fotodosen')->move($destination, $photo);
    $data['fotodosen'] = $photo;
    Dosen::create($data);

create :

 {!! Form::open(array('fotodosen'=>'create', 'method'=>'POST', 'files'=>true, 'url'=>'uploads')) !!}
             {!! Form::file('image') !!}
                   <div class="form-group">
                        <div class="col-md-6 col-md-offset-4">
                            <button type="submit" class="btn btn-primary">
                                <i class="fa fa-btn fa-user"></i> Register
                            </button>
                             {!! Form::close() !!}

already edit method to :

 $photo = $request->file('fotodosen')->getClientOriginalName($photo);

still got that error. what am i missing?

UPDATE :

public function store(CreateDosenRequest $request)

{



    $user = User::create([
        'name' => $request->input('name'),
        'username' => $request->input('username'),
        'email' => $request->input('email'),
        'password' => $request->input('password'),
        'admin' => $request->input('admin'),
     ]);

      $dosen = Dosen::create([
        'iddosen' => $request->input('iddosen'),
        'nipy' => $request->input('nipy'),
        'namadosen' => $user->name,
        'user_id' => $user->id,
        'alamatdosen' => $request->input('alamatdosen'),
        'notelpdosen' => $request->input('notelpdosen'),
        'tempatlahirdosen' => $request->input('tempatlahirdosen'),
        'tanggallahirdosen' => $request->input('tanggallahirdosen'),
        'agamadosen' => $request->input('agamadosen'),

    ]);
        if ($request->hasFile('image')) {
            $data = $request->input('image');
            $photo = $request->file('image')->getClientOriginalName();
            $destination = public_path() . '/uploads/';
            $request->file('image')->move($destination, $photo);
            $data['fotodosen'] = $photo;
            Dosen::create($data);
 }

You have the file name as image try to use image instead of fotodosen

$photo = $request->file('image')->getClientOriginalName();

Full code

$data = $request->input('image');
$photo = $request->file('image')->getClientOriginalName();
$destination = base_path() . '/public/uploads';
$request->file('image')->move($destination, $photo);

You can check for the file like,

if ($request->hasFile('image')) {
    // your code here
}

From Http Requests and an article file upload in laravel 5

I had the same issue so I solved it using html form attribute enctype="multipart/form-data"

Example

<form name="le_form" action="/ft" method="POST" enctype="multipart/form-data">

For more information see

For me, what I found out is that i did not included the enctype'=>'multipart/form-data on the form. When i did, it eventually solved the problem.

just do this if you are uploading a file or have a file field inside your form

 {{  Form::open(array('url' => 'dashboard/new_job', 'enctype'=>'multipart/form-data')) }}

OR

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM