Laravel 5.7 - 上传到公共文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51946465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.7 - upload to public folder
提问by Vincent Decaux
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.
我可以使用该store
功能吗?我想要相同的行为:文件名被散列,文件夹是动态创建的。
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 ?
但是我怎样才能填充$content
变量?如何轻松地对文件名进行散列处理?
回答by Erubiel
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...
这会将您的文件移动到 public/uploads 文件夹...而不是 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(''));