23
Route::get('marquee', function(){
    echo  File::get('\storage\app\marquee.json');
});

I have a json file place inside storage/app

My question is how can I read this content from controller or route?

4
  • Did you tried File::get('marquee.json') ? Commented Mar 29, 2017 at 11:19
  • not working too, cant get correct file path Commented Mar 29, 2017 at 11:20
  • Try this File::get(asset('storage/app/marquee.json')); Commented Mar 29, 2017 at 11:25
  • Where's the file? /storage/app/ implies that it's the the filesystem root. Commented Mar 29, 2017 at 11:26

6 Answers 6

53

Using Storage facade:

Storage::disk('local')->get('marquee.json');

The old way, using File facade (deprecated for Laravel 7):

File::get(storage_path('app/marquee.json'));
Sign up to request clarification or add additional context in comments.

4 Comments

For those wanting to access files at the project root (above the storage directory), use: File::get(base_path() . 'marquee.json')
I am using Laravel 7, File::get() requires an import, what do I import? I want to import data.json from the database/data directory in the project @JarrettBarnett
Seems File might be deprecated for Laravel 7. Use Storage::get() instead. laravel.com/docs/7.x/filesystem#retrieving-files
@ChrisClaude the FIle facade has namespace Illuminate\Support\Facades
7

You can go with the absolute path

\Illuminate\Support\Facades\File::get(base_path() . '/storage/app/marquee.json');

1 Comment

In our Vapor deployed app, we needed to access a json file inside our seeders and no solutions worked, except we putting base_path() in-front of the file path as above. FYI.
6

Try this code

File::get(storage_path('app\marquee.json'));

Comments

5

You can use storage_path() function for locate the storage folder and then join app folder name like that:

$path = storage_path() . "/app/marquee.json";
echo  File::get($path);

Comments

3

From external source use Http requests, documentation here, eg:

$response = Http::get('http://test.com');

Comments

2

You can store files in your storage folder in Laravel:

$path = storage_path() . "/json/${filename}.json";

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.