towhid's avatar

BadMethodCallException Method Illuminate\Http\UploadedFile::getClientOrginalName does not exist.

this is my #controller

public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required',
            'body' => 'required',
            'cover_image' => 'image|nullable|max:1999',
        ]);

        // Handle File upload
        if($request->hasFile('cover_image')){
            // get filename with the extention
            $fileNameWithExt = $request->file('cover_image')->getClientOrginalName();

            // get just filename 
            $fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);

            // get just Ext
            $extention = $request->file('cover_image')->getClientOrginalExtention();

            // Filename to store
            $fileNameToStore = $filename.'_'.time().'.'.$extention;

            // upload image
            $path = $request->file('cover_image')->storeAs('public/cover_images',$fileNameToStore);
        }else{
            $fileNameStore='noimage.jpg';
        }        
        //create post
        $post=new Post;
        $post->title = $request->input('title');
        $post->body = $request->input('body');
        $post->user_id = auth()->user()->id;
        $post->cover_image = $fileNameToStore;
        $post->save();

        return redirect('/posts')->with('success','Post created');

    }

why this error occurd ?

0 likes
12 replies
bobbybouwmann's avatar
Level 88

It should be getClientOriginalExtension instead of getClientOrginalExtention.

The t should be an s in extension

3 likes
towhid's avatar

@bobbybouma after correction some code -

 $fileNameWithExt = $request->file('cover_image')->getClientOriginalName();

$extention = $request->file('cover_image')->getClientOriginalExtension();

out put is

The cover image failed to upload.

Snapey's avatar

How could we possibly say?

You could just have an echo statement 'The cover image failed to upload.' ?

Sergiu17's avatar

It's related to validation rules

'uploaded' => 'The :attribute failed to upload.',

in validation.php

1 like
towhid's avatar

@snapey hello , when iam trying to upload image size 1999 then show this error . could you please tell me how to maximize the image size controll ?

Snapey's avatar

In your validation you are saying that the file cannot be larger than 1.999 MB

In addition, PHP has upload_max_filesize and also post_max_size

You need to understand what these are to know if your upload will work

1 like
towhid's avatar

@Snapey hi , i already use max: 1999 to max : 5000 but still same error

The cover image failed to upload.

Sergiu17's avatar

@towhid ok, this is first layer. what do you have in php.ini ? as @Snapey said upload_max_filesize and also post_max_size

Cronix's avatar

Try uploading a small image, like one that's less than 1mb. Just to test if the upload functionality works.

If it does work for smaller files, then you need to change your php.ini settings and reboot the web server like the others have mentioned.

Yes, it's most likely a php settings issue, not laravel. If the php settings don't allow uploads greater than 2MB, then, simply, it won't work if the image is greater than that size in Laravel or anything else. PHP itself is preventing it.

Cronix's avatar

You can easily see the php settings by creating this route and going to it.

Route::get('/phpinfo', function() {
    phpinfo();
});

It will make a nice HTML page of all of the php settings and enabled extensions. Be sure to delete the route when you're done.

Please or to participate in this conversation.