Search code examples
phplaravelfile-upload

File upload error Call to a member function getClientOriginalExtension() on string


Call to a member function getClientOriginalExtension() on string this error come when i run my code

public function store(Request $request){
    $file_ex=$request -> photo -> getClientOriginalExtension();

    $file_name = $file_ex.time();

    $path ='/images/users';
    
    $request -> photo ->move($path, $file_name);

where the problem

file uploade , find out why this not working


Solution

  • Okay, this is the answer, or what I made to solve this issue.

    At first, make sure of 3 things:

    1. Make sure that your form works with files.

    in enctype="multipart/form-data" put it on form #tag like:

    <form action="{{url('store')}}" method="POST" enctype="multipart/form-data">
    
    1. Make sure that your function in your controller works fine, like
    $file = $request->photo;
    $file_ex =$file->getClientOriginalExtension();
    $filename = time().rand(1,99).'.'.$file_ex;
    $path ='images/users';
    $file->move(public_path($path),$filename);
    

    Okay, this works fine.

    1. Handling our file system

    go to config/filesystems.php

    and scroll down until you will find  'disks' => [

    In disks, you will find 'local' , 'public', and 's3' copy public and paste it on side 'public' and make some changes like:

    At first, we will see

    public :

    'public' => [
                'driver' => 'local',
                'root' => storage_path('app/public'),
                'url' => env('APP_URL').'/storage',
                'visibility' => 'public',
                'throw' => false,
            ],
            
    
    1. Change the root, like 'root' => base_path().'/public/images/users', or you can put which path you want.

    2: change the url, like 'url' => env('APP_URL').'/public', or you can specify which URL you want.   The code will be:

    'users' => [
        'driver' => 'local',
        'root' => base_path().'/public/images/users',
        'url' => env('APP_URL').'/public',
        'visibility' => 'public',
        'throw' => false,
    ], 
    

    Thank you for your assistance, If there are any issues, just ask.