Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hadis's avatar
Level 1

Call to a member function getClientOriginalName() on null

i want upload image and save it in a directoty but i get this error,what should i do? this is my code:

public function ImageUploader($file)
    {
        $fileName = time() . "_" . $file->getClientOriginalName();
        $path = public_path('/uploads/');
        $files = $file->move($path, $fileName);
        $img = Image::make($files->getRealPath());
        $img->resize(259, 204);
        $img->save($path . "small-" . $fileName);
        return "/uploads/" . $fileName;
    }```


 ```public function store(Request $request)
    {
        $file=$request['image'];
        $img = $this->ImageUploader($file);
        Article::create([
            'small_explain'=>$request['small_explain'],
            'title'=>$request['title'],
            'body'=>$request['body'],
            'important_body'=>$request['important_body'],
            'quote'=>$request['quote'],
            'author_quote'=>$request['author_quote'],
            'index_image' => $img,
            'header_image' => $img,
            'text_image' => $img,
        ]);
        return redirect(route('admin.articles.index'));
    }```
0 likes
3 replies
munazzil's avatar

Have you test with below command and after check it?

     php artisan storage:link
Sinnbeck's avatar

I think you are missing some code? In the first code block $file is never set to anything?

Nakov's avatar

$file is null in your case..

Change this line

$file=$request['image'];

With

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

And make sure that a file has been uploaded at all. Make sure you are not missing the enctype attribute on your form in the view.

1 like

Please or to participate in this conversation.