skip to Main Content

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

2

Answers


  1. Please, try like this,

        $file = $request->file('photo');
        $file_ex = $file->getClientOriginalExtension();
        $filename = time().rand(1,99).'.'.$file_ex;
        $path ='images/users';
    
        $file->move(public_path($path),$filename);
    
    Login or Signup to reply.
  2. first run this command to generate symbolic link

    php artisan storage:link
    

    then in controller function

    $fileTemp = $request->file('file');
    if($fileTemp->isValid()){
      $fileExtension = $fileTemp->getClientOriginalExtension();
      $fileName = Str::random(4). '.'. $fileExtension;
      $path = $fileTemp->storeAs(
        'public/documents', $fileName
      );
    }
    

    above code will save your file in storage/app/public/documents location
    and using symbolic link we can access this file here public/storage/documents

    read file-uploads full docs in laravel official docs

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search