0

After going through a lot of documentation I am able to save an image in Laravel.

But now I am getting an error that every time I save the image it got saved in a separate folder created for that image.

So if I save an image it got saved like

http://localhost/storage/uploads/phpAC6E.tmp.jpg/gedmQBjYgGecrqiuJol6wQs0BKkkMuCko91opWvi.jpeg

Notice the folder phpAC6E.tmp.jpg got created automatically.

The question here is not what I am looking for.

I don't know where I am going wrong I had tried many things but it doesn't work. Below are my code snippets :

filesystem.php configuration-

'local' => [
            'driver' => 'local',
            'root' => storage_path('app/public/uploads'),
            'url' => env('APP_URL').'/storage/uploads',
            'visibility' => 'private',
        ],

My controller function-

public function save(Request $request){

        $file = $request->file('image');

        $ext = $file->getClientOriginalExtension();
        $path = Storage::disk('local')->put($file->getFilename().'.'. $ext,  $request->file('image'));

        return Storage::url($path);

    }

The folders have been created like this.

Ishaan
  • 1,249
  • 15
  • 26
  • Possible duplicate of [laravel 5.4 upload image](https://stackoverflow.com/questions/42755302/laravel-5-4-upload-image) – Zakaria Acharki May 30 '19 at 13:55
  • @ZakariaAcharki my question is different from the one you mentioned, my error is different also I am using **Storage** facade. Help me through this. – Ishaan May 31 '19 at 13:57

1 Answers1

0

why not try this code. its working properly. it saves in the public folder by creating first attachment folder outside the root of the domain and saving the file inside. if you would want to save inthe public folder then change to:

$file->move(public_path('attachments'), $name);

if($request->hasfile('filename'))
{
   $file=$request->file('filename');

   $name=$file->getClientOriginalName();
   $file->move(public_path().'/attachments', $name);
   DB::table('tblname')->insert([
   'filename' => $name,
 ]);

}
Julius Fasema
  • 917
  • 6
  • 13
  • Maybe this code will work but I am following as what is written in the **documentation**. So I'll be thankful if you can tell me the error I am doing in my code. – Ishaan Jun 01 '19 at 14:21