Laravel Get File Contents from Storage Example
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 10 Import Large CSV File into Database Example
- Laravel - How to Get All Files in a Directory?
- Laravel Download File from URL to Storage Example
- How to Add Password Protection for PDF File in Laravel?
- Laravel Seeder from CSV File Example
- Laravel Amazon S3 File Upload Tutorial
- Laravel Blade Include File Example
- Laravel Livewire File Upload Tutorial
- How to Create File Object from Path in Laravel?
- How to Delete File from Public Folder / Storage Folder in Laravel?
- How to Display Image from Storage Folder in Laravel?
- Laravel Vue JS File Upload Example
- Laravel Storage Dropbox Integration Example