1

I am new in larval development, I am unable to upload image in local folder and I am getting error getClientOriginalExtension() on null. Even I have added code enctype="multipart/form-data" in my form, but still am getting the error. Can anyone give suggestion how to resolve this issue. I have given below my code. MyBlade file

        ****MyBlade file****

                   <form action="{{ route('register.post') }}" method="POST" enctype="multipart/form-data" accept-charset="utf-8"  >
                      @csrf
                     
                      <div class="form-group row">
                          <label for="uname" class="col-md-4 col-form-label text-md-right">User Name</label>
                          <div class="col-md-6">
                              <input type="text" id="username" class="form-control" name="username" required />
                              @if ($errors->has('username'))
                                  <span class="text-danger">{{ $errors->first('username') }}</span>
                              @endif
                          </div>
                      </div>
                       <div class="form-group row{{ $errors->has('image') ? ' has-error' : '' }}">
                        <label for="name" class="col-md-4 col-form-label text-md-right">Profile Picture</label>
                        <div class="col-md-6">
                        <input id="image" type="file" class="form-control" name="image">
                    </div>
                 </div>

My Controller File

       if ($request->input('image')!=null){
        $files = $request->input('image');

        $extension = $files->getClientOriginalExtension();
        $filaname = time().','.$extension;
        $files->move('public/image/',$filaname);
       // $post->image= $filaname;
     }
       else{
         return $request;
       }
        




          

I am getting err:Call to a member function getClientOriginalExtension() on bool

5
  • Why are you assigning $request->hasfile('image') to $files. Even if you meant to compare it, it makes no sense. Commented Nov 13, 2021 at 5:09
  • HI Thank you for your response. I have edited my code now, please check now it's going to else condition. Can you give solution for this issue? Commented Nov 13, 2021 at 5:25
  • Just read the docs Commented Nov 13, 2021 at 5:29
  • I think this - $files = $request->input('image'); - should be $files = $request->file('image'); OR $files = $request->image; Commented Nov 13, 2021 at 8:36
  • Yes actually image was uploaded successfully into the local folder but database table stored image name like this "C:\xampp\tmp\php2D22.tmp" Commented Nov 13, 2021 at 10:01

1 Answer 1

2

If you use $request->file('image') instead of $request->input('image') you have fixed the problem. The image will then be moved to the public/image folder.

However, if you want to upload the file to the storage/public folder, you can use the storeAs() method on the $file to store it in the storage folder.

// Your controller
public function update(Request $request, $postId)
{
    if (! $request->hasFile('image')) {
        return $request;
    }

    // Use file() instead of input()
    $file = $request->file('image');

    $extension = $file->getClientOriginalExtension();

    // Note you have a typo in your example, you're using a `,` instead of a `.`
    $filename = time().'.'.$extension;

    // To store (move) the file to the 'public/image' folder, use this:
    $file->move('image', $filename);

    // To store the file to the 'storage/app/public/image' folder, use this:
    $file->storeAs('public/image', $filename);

    // Find your post based on some id
    $post = Post::find($postId);

    // Save the filename to the database on the Post model:
    $post->image = $filename;
    $post->save();
}

Hope I helped you with this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.