Laravel5 Json 获取文件内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35374699/
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-03 18:17:07  来源:igfitidea点击:

Laravel5 Json get file contents

jsonlaravelfile-get-contents

提问by KMackinley

I'm using Laravel-5right now and I've just created a JSONfile called test(test.json). My question is:

我现在正在使用Laravel-5,我刚刚创建了一个JSON名为 test(test.json)的文件。我的问题是:

  1. Where should I put my JSON file?
  2. How can I use file_get_contents and point to that one file I wanted?
  1. 我应该把我的 JSON 文件放在哪里?
  2. 如何使用 file_get_contents 并指向我想要的那个文件?

This is what I've tried which is obviously wrong:

这是我尝试过的显然是错误的:

$string = file_get_contents("../json/test.json");   
$json_file = json_decode($string, true);

Thank you so much for helping!

非常感谢您的帮助!

回答by Jilson Thomas

You can store it in your storage folder in Laravel:

您可以将其存储在 Laravel 的存储文件夹中:

$path = storage_path() . "/json/${filename}.json"; // ie: /var/www/laravel/app/storage/json/filename.json

$json = json_decode(file_get_contents($path), true);