3

I am having trouble uploading a user image to my public folder. The file name generates correctly, and saves the name to my database, except the iamge itself refuses to get saved into my public folder. What am I doing wrong?

  public function update_avatar(Request $request) {
  if($request->hasFile('avatar')) {

    $avatar = $request->file('avatar');
    $filename = time() . "." . $avatar->getClientOriginalExtension();

    Image::make($avatar)->resize(300,300)->save(public_path('/uploads/'.$filename)); ==> This is causing me errors

    user = Auth::user();
    $user->avatar = $filename;
    $user->save();

  }
3
  • Does this help at all? laracasts.com/discuss/channels/laravel/… Commented Oct 12, 2017 at 13:01
  • What does public_path('/ uploads ...) return? What is the path it tries to save to? Commented Oct 12, 2017 at 13:02
  • which error are you get? please describe your error. So i will help you Commented Oct 12, 2017 at 13:03

3 Answers 3

1

The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in storage/app/public. To make them accessible from the web, you should create a symbolic link from public/storage to storage/app/public. This convention will keep your publicly accessible files in one directory that can be easily shared across deployments.

To create the symbolic link, you may use the storage:link Artisan command:

php artisan storage:link

https://laravel.com/docs/5.5/filesystem

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

Comments

1

I think you should try this:

$destinationPath = public_path('uploads');

Image::make($avatar)->resize(300,300)->save($destinationPath.'/'.$filename);

Comments

0

First you must move your image to destination directory and then resize it.

$avatar->move(public_path('/uploads/'.$filename));
Image::make(public_path('/uploads/'.$filename))->resize(300,300)->save(public_path('/uploads/'.$filename));

Note

Check the directory that you moving your file there is already exist.

1 Comment

When I do this, it does not move the image correctly: I end up getting a folder with the image name (sakjdha.jpg), and then a jibberish document within it (ASCII), and the result (1/1) NotReadableException Unable to find file ().

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.