Benjamin W
Benjamin W

Reputation: 2848

Laravel get file content

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?

Upvotes: 23

Views: 130201

Answers (6)

Alexey Mezenin
Alexey Mezenin

Reputation: 163968

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'));

Upvotes: 53

Adam Pery
Adam Pery

Reputation: 2102

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

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

Upvotes: 3

Hiren Makwana
Hiren Makwana

Reputation: 2156

You can store files in your storage folder in Laravel:

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

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

Upvotes: 2

Rahman Qaiser
Rahman Qaiser

Reputation: 682

You can go with the absolute path

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

Upvotes: 7

Emtiaz Zahid
Emtiaz Zahid

Reputation: 2855

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);

Upvotes: 5

Vladimir Makarov
Vladimir Makarov

Reputation: 116

Try this code

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

Upvotes: 6

Related Questions