Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: How to save images to public folder?

Tags:

upload

laravel

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

  }
like image 859
jlim Avatar asked Nov 19 '25 12:11

jlim


2 Answers

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

like image 113
Alexey Mezenin Avatar answered Nov 21 '25 06:11

Alexey Mezenin


I think you should try this:

$destinationPath = public_path('uploads');

Image::make($avatar)->resize(300,300)->save($destinationPath.'/'.$filename);
like image 44
AddWeb Solution Pvt Ltd Avatar answered Nov 21 '25 06:11

AddWeb Solution Pvt Ltd