'Laravel 5.7 - upload to public folder
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 ?
Solution 1:[1]
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(''));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Erubiel |
