0
0
Laravelframework~5 mins

Local disk storage in Laravel

Choose your learning style9 modes available
Introduction

Local disk storage lets your Laravel app save files directly on your server's hard drive. This is useful for storing images, documents, or backups safely and quickly.

You want to save user-uploaded photos or files on your own server.
You need to store temporary files during processing.
You want to keep backups or logs locally for easy access.
You are developing on your local machine and want to test file storage.
You want fast access to files without relying on external services.
Syntax
Laravel
use Illuminate\Support\Facades\Storage;

// Save a file
Storage::disk('local')->put('filename.txt', 'File contents');

// Get a file
$content = Storage::disk('local')->get('filename.txt');

// Delete a file
Storage::disk('local')->delete('filename.txt');

The 'local' disk stores files in the storage/app folder by default.

Use the Storage facade to interact with files easily.

Examples
This saves a file named notes.txt with the text 'Hello Laravel!' inside the local storage folder.
Laravel
Storage::disk('local')->put('notes.txt', 'Hello Laravel!');
This reads the content of notes.txt from local storage and prints it.
Laravel
$content = Storage::disk('local')->get('notes.txt');
echo $content;
This deletes the notes.txt file from local storage.
Laravel
Storage::disk('local')->delete('notes.txt');
Sample Program

This Laravel controller has three methods: one to save a file named example.txt with some text, one to read and return its content, and one to delete the file. It uses the local disk storage.

Laravel
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class FileController extends Controller
{
    public function saveFile()
    {
        Storage::disk('local')->put('example.txt', 'This is a test file.');
        return 'File saved successfully!';
    }

    public function readFile()
    {
        if (Storage::disk('local')->exists('example.txt')) {
            $content = Storage::disk('local')->get('example.txt');
            return $content;
        }
        return 'File not found.';
    }

    public function deleteFile()
    {
        Storage::disk('local')->delete('example.txt');
        return 'File deleted successfully!';
    }
}
OutputSuccess
Important Notes

Files saved on the local disk are stored in storage/app. You can change this path in config/filesystems.php.

Remember to run php artisan storage:link if you want to make files publicly accessible via public/storage.

Local disk storage is fast but only works on the server where your app runs.

Summary

Local disk storage saves files on your server's hard drive inside storage/app.

Use the Storage facade with disk('local') to put, get, and delete files.

This method is great for quick, private file storage during development or on your own server.