1

I know this question has been posted several times, but I can't get it.

Now I use :

 // photo upload
 $dir = strtolower(str_random(2));
 $image = $request->file('image')->store('products/'. $dir);

It works perfectly, my file is uploaded to :

storage/app/products/ds/HASH.jpg

But it's not public, I saw the documentation, but I can't understand how to upload to public folder easily.

Can I use the store function ? I want the same behavior : the filename is hashed, the folder is created on the fly.

I tried :

Storage::setVisibility($image, 'public');

But it doesn't work (no error...).

The doc says to use :

Storage::put('file.jpg', $contents, 'public');

But how can I fill the $content variable ? How can I have the filename hashed easily ?

miken32
  • 42,008
  • 16
  • 111
  • 154
Vincent Decaux
  • 9,857
  • 6
  • 56
  • 84
  • Have you run `php artisan storage:link` to create the public storage symlink? – Jamesking56 Aug 21 '18 at 10:44
  • Why not using Intervention? http://image.intervention.io/ – Vipertecpro Aug 21 '18 at 11:03
  • Yes the link is created, but the link uses `storage/app/public/` folder, now my images are in `storage/app/` folder – Vincent Decaux Aug 21 '18 at 11:14
  • Does this answer your question? [How to upload files in Laravel directly into public folder?](https://stackoverflow.com/questions/44577380/how-to-upload-files-in-laravel-directly-into-public-folder) – miken32 Nov 09 '21 at 18:05

1 Answers1

0

I usually use the move function, something like this should work:

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

    $file_name = time() . $file->getClientOriginalName();                      

    $file_path = 'uploads/';

    $file->move($file_path, $file_name);

This would move your file to the public/uploads folder... instead of storage/app/public...

You can also access the storage files through something like this, but for this you'll have to add a route and method for file access

    return response()->file(storage_path(''));
Erubiel
  • 2,934
  • 14
  • 32