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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:23:04  来源:igfitidea点击:

How to get public directory?

laravellaravel-4

提问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 $pathhow 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

I know this is a little late, but if someone else comes across this looking, you can now use public_path(); in Laravel 4, it has been added to the helper.php file in the support folder see here.

我知道这有点晚了,但是如果其他人遇到这种情况,您现在可以使用 public_path(); 在 Laravel 4 中,它已添加到 support 文件夹中的 helper.php 文件中,请参见此处

回答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