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

Published 6 days ago by towhid

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 ?

bobbybouwmann

It should be getClientOriginalExtension instead of getClientOrginalExtention.

The t should be an s in extension

towhid
towhid
6 days ago (3,030 XP)

@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
Snapey
6 days ago (922,945 XP)

How could we possibly say?

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

Sergiu17

It's related to validation rules

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

in validation.php

towhid
towhid
6 days ago (3,030 XP)

@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 ?

Sergiu17

@towhid in php.ini increase upload_max_filesize

towhid
towhid
5 days ago (3,030 XP)

o so its depend on - php.ini settings, not any laravel -- end ? @Sergiu17

Snapey
Snapey
4 days ago (922,945 XP)

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

towhid
towhid
3 days ago (3,030 XP)

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

The cover image failed to upload.

Sergiu17

@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
Cronix
3 days ago (325,600 XP)

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
Cronix
3 days ago (325,600 XP)

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 sign in or create an account to participate in this conversation.