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();

  }
jlim
  • 91
  • 4
  • 11

3 Answers3

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

Community
  • 1
  • 1
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
1

I think you should try this:

$destinationPath = public_path('uploads');

Image::make($avatar)->resize(300,300)->save($destinationPath.'/'.$filename);
halfer
  • 19,824
  • 17
  • 99
  • 186
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
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.

Saeed Vaziry
  • 2,195
  • 5
  • 26
  • 39
  • 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 (). – jlim Oct 12 '17 at 14:22