Laravel5 Json get file contents

KMackinley picture KMackinley · Feb 13, 2016 · Viewed 31.3k times · Source

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

  1. Where should I put my JSON file?
  2. How can I use file_get_contents and point to that one file I wanted?

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!

Answer

Jilson Thomas picture Jilson Thomas · Feb 13, 2016

You can store it in your storage folder in Laravel:

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

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