简体   繁体   中英

call to a member function getClientOriginalName() on null using laravel

I keep on getting the following error when I try to upload images. Does anybody have any idea how to solve this? I'm confused about what to do next.

Call to a member function getClientOriginalName() on null

https://flareapp.io/share/dmkydl73#F42

Controller

public function singalprojectaction(Request $request)
{
    $validation = validator::make($request->all(), [
        'select_file_one' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        'select_file_Two' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        'select_file_Three' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        'select_file_Four' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        'select_file_Five' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

    if ($validation->passes()) {
        $select_project = $request->get('select_project');
        $project_name = $request->get('project_name');
        $Client_name = $request->get('Client_name');
        $Completion_date = $request->get('Completion_date');
        $Duration = $request->get('Duration');
        $Description = $request->get('Description');
        $select_file_one = $request->file('select_file_one');
        $select_file_Two = $request->file('select_file_Two');
        $select_file_Three = $request->file('select_file_Three');
        $select_file_Four = $request->file('select_file_Four');
        $select_file_Five = $request->file('select_file_Five');

        $new_name_one = mt_rand().'.'.$select_file_one->getClientOriginalName();
        $select_file_one->move(public_path('projects'), $new_name_one);
        $new_name_Two = mt_rand().'.'.$select_file_Two->getClientOriginalName();
        $select_file_Two->move(public_path('projects'), $new_name_Two);
        $new_name_Three = mt_rand().'.'.$select_file_Three->getClientOriginalName();
        $select_file_Three->move(public_path('projects'), $new_name_Three);
        $new_name_Four = mt_rand().'.'.$select_file_Four->getClientOriginalName();
        $select_file_Four->move(public_path('projects'), $new_name_Four);
        $new_name_Five = mt_rand().'.'.$select_file_Five->getClientOriginalName();
        $select_file_Five->move(public_path('projects'), $new_name_Five);

        $query = DB::table('single_portfolio')->insert([
            'project_id' => $select_project, 'Project_name' => $project_name,
            'Client_Name' => $Client_name, 'Completion_date' => $Completion_date
            , 'Duration' => $Duration, 'Description' => $Description, 'image_one' => $new_name_one, 'image_two' =>
                $new_name_Two,
            'image_three' => $new_name_Three, 'image_four' => $new_name_Four, 'image_five' => $new_name_Five
        ]);

        if ($query) {
            return redirect()->back()->with('messages', 'Data Is Successfully Inserted');
        }

    }

    return redirect()->back();
}

From what I can tell the $request->file('select_file_Two'); returns null ; This means most likely that no files was selected for the input with name select_file_Two , make sure the names is the same casing and not select_file_two . If all files are required you can add a required to the validator like so

 `require|image|mimes:jpeg,png,jpg,gif,svg|max:2048`

or if the are optional you could check if the file exist before hand like so

if(!is_null($select_file_one)){
  $new_name_one = rand() . '.' . $select_file_one->getClientOriginalName();
  $select_file_one->move(public_path('projects'), $new_name_one);
}

and skill all the files that were not send.

Also something to note here is you are using rand() to generate the the new name this may in time overwrite files. I would suggest adding a timestamp like so

use Carbon\Carbon;
$name = rand() . '_' . now()->timestamp . '.' . $image->getClientOriginalExtension();

if your codes are ok , which seems so, most of the time is you html form. take a look at your form and see if enctype="multipart/form-data" is included in your form tag, otherwise the files wont be sent.the hole form part should be like this :

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

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