Msoft's avatar
Level 1

How to fix Call to a member function getClientOriginalExtension() on null?

working with laravel 5.6 and I have images update function in My edit.blade.php file,

edit.blade.php

<img id="preview"
                         src="{{asset((isset($upload) && $upload->resized_name!='')?'images/'.$upload->resized_name:'images/noimage.png')}}"
                         height="200px" width="200px"/>
                    <input class="form-control" style="display:none" name="files[]" type="file" id="files" name="_token" value="{{ csrf_token() }}" enctype="multipart/form-data">
                    <br/>

and My update Controller is

 $photos = $request->file('files');
 
        if (!is_array($photos)) {
            $photos = [$photos];
        }
 
        if (!is_dir($this->photos_path)) {
            mkdir($this->photos_path, 0777);
        }
 
        for ($i = 0; $i < count($photos); $i++) {
            $photo = $photos[$i];
            $name = sha1(date('YmdHis') . str_random(30));
            $save_name = $name . '.' . $photo->getClientOriginalExtension();
            $resize_name = $name . str_random(2) . '.' . $photo->getClientOriginalExtension();
 
            Image::make($photo)
                ->resize(250, null, function ($constraints) {
                    $constraints->aspectRatio();
                })
                ->save($this->photos_path . '/' . $resize_name);
 
            $photo->move($this->photos_path, $save_name);
 
            $upload = Upload::find($id);
            $upload->filename = $save_name;
            $upload->resized_name = $resize_name;
            $upload->original_name = basename($photo->getClientOriginalName());
            $upload->vehicle_id = $vehicle->id;
            $upload->save();
        }

My problem is when I go to edit form and edit some onother field values without image edit (existing images) and click update buttons generated following error message,

1/1) FatalErrorException

Call to a member function getClientOriginalExtension() on null
in VehicleController.php line 376

VehicleController.php line 376 is this,

$save_name = $name . '.' . $photo->getClientOriginalExtension();

how can I fix this problem?

0 likes
4 replies
Cronix's avatar
<input class="form-control" style="display:none" name="files[]" type="file" id="files" name="_token" value="{{ csrf_token() }}" enctype="multipart/form-data">

Where is your <form> open tag? That's what should have enctype="multipart/form-data", not the input.

if (!is_array($photos)) {
    $photos = [$photos];
}

don't need that. It will always be an array because name="files[]"

change

for ($i = 0; $i < count($photos); $i++) {
    $photo = $photos[$i];

to just

foreach ($photos as $photo)
Msoft's avatar
Level 1

@Cronix did you mean,

$photos = $request->file('files');
for ($i = 0; $i < count($photos); $i++) {
            $photo = $photos[$i];
            $name = sha1(date('YmdHis') . str_random(30));
            $save_name = $name . '.' . $photo->getClientOriginalExtension();
            $resize_name = $name . str_random(2) . '.' . $photo->getClientOriginalExtension();
 
            Image::make($photo)
                ->resize(250, null, function ($constraints) {
                    $constraints->aspectRatio();
                })
                ->save($this->photos_path . '/' . $resize_name);
 
            $photo->move($this->photos_path, $save_name);
 
            $upload = Upload::find($id);
            $upload->filename = $save_name;
            $upload->resized_name = $resize_name;
            $upload->original_name = basename($photo->getClientOriginalName());
            $upload->vehicle_id = $vehicle->id;
            $upload->save();
        }
gasparyanyur's avatar

As I can see you dont have declared <form>. You need to declare form

<form method = "POST" enctype="multipart/form-data">
        <input type="file" />
                {{csrf_field()}}
</form>

Please or to participate in this conversation.