php Laravel 获取文件内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43091997/
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 get file content
提问by Benjamin W
Route::get('marquee', function(){
echo File::get('\storage\app\marquee.json');
});
I have a json file place inside storage/app
我在存储/应用程序中有一个 json 文件
My question is how can I read this content from controller or route?
我的问题是如何从控制器或路由读取此内容?
回答by Alexey Mezenin
Using Storage
facade:
使用Storage
门面:
Storage::disk('local')->get('marquee.json');
The old way, using File
facade (deprecated for Laravel 7):
旧方式,使用File
外观(Laravel 7 已弃用):
File::get(storage_path('app/marquee.json'));
回答by Vladimir Makarov
Try this code
试试这个代码
File::get(storage_path('app\marquee.json'));
回答by Emtiaz Zahid
You can use storage_path()function for locate the storage folder and then join app folder name like that:
您可以使用storage_path()函数来定位存储文件夹,然后像这样加入应用程序文件夹名称:
$path = storage_path() . "/app/marquee.json";
echo File::get($path);
回答by Rahman Qaiser
You can go with the absolute path
您可以使用绝对路径
\Illuminate\Support\Facades\File::get(base_path() . '/storage/app/marquee.json');
回答by Hiren Makwana
You can store files in your storage folder in Laravel:
您可以将文件存储在 Laravel 的存储文件夹中:
$path = storage_path() . "/json/${filename}.json";
$json = json_decode(file_get_contents($path), true);