Laravel Get File Contents from Storage Example

By Hardik Savani November 5, 2023 Category : Laravel

Hi,

This article will give you an example of laravel storage get file content. you'll learn laravel get file content from public folder. Here you will learn laravel get file content from storage. I explained simply step by step how to get file content in laravel. Alright, let’s dive into the details.

Laravel offers the Storage and File facades for managing file systems. In this guide, I will demonstrate how to retrieve file content from both the storage and public folders within Laravel. You can refer to the following examples to learn how to access file content.

Review following one by one example:

Example 1: Laravel Get File Contents using Storage

here is a controller code of getting file content from Storage facade.

app/Http/Controllers/FileController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Storage;

class FileController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$contents = Storage::get('upload/test.txt');

dd($contents);

}

}

Output:

demo.com content

Example 2: Laravel Get File Contents using File

here is a controller code of getting file content from File facade.

app/Http/Controllers/FileController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use File;

class FileController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$contents = File::get(public_path('upload/test.txt'));

dd($contents);

}

}

Output:

demo.com content

I hope it can help you...

Tags :
Shares