laravel 如何获取公共目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14837065/
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
How to get public directory?
提问by Yasser Moussa
I am a beginner here so pardon me for this question am using return File::put($path , $data);
to create a file in public folder on Laravel. I used this piece of code from controller I need to know the value of $path
how should it be.
我是这里的初学者,所以请原谅我使用这个问题return File::put($path , $data);
在 Laravel 的公共文件夹中创建文件。我使用了控制器中的这段代码,我需要知道$path
它应该如何值。
回答by Justin
Use public_path()
用 public_path()
For reference:
以供参考:
// Path to the project's root folder
echo base_path();
// Path to the 'app' folder
echo app_path();
// Path to the 'public' folder
echo public_path();
// Path to the 'storage' folder
echo storage_path();
// Path to the 'storage/app' folder
echo storage_path('app');
回答by Laurence
You can use base_path() to get the base of your application - and then just add your public folder to that:
您可以使用 base_path() 获取应用程序的基础 - 然后将您的公共文件夹添加到该文件夹中:
$path = base_path().'/public';
return File::put($path , $data)
Note: Be verycareful about allowing people to upload files into your root of public_html. If they upload their own index.php file, they will take over your site.
注意:在允许人们将文件上传到您的 public_html 根目录时要非常小心。如果他们上传自己的 index.php 文件,他们将接管您的网站。
回答by Brent
回答by GCT
The best way to retrieve your public folder path from your Laravel config is the function:
从 Laravel 配置中检索公共文件夹路径的最佳方法是以下函数:
$myPublicFolder = public_path();
$savePath = $mypublicPath."enter_path_to_save";
$path = $savePath."filename.ext";
return File::put($path , $data);
There is no need to have all the variables, but this is just for a demonstrative purpose.
没有必要拥有所有变量,但这只是为了演示目的。
Hope this helps, GRnGC
希望这会有所帮助,GRnGC