Laravel Public Direct Disk Configuration

Learn how to configure and use the public_direct disk in Laravel for direct file access in the public directory without the storage slug.

Step 1: Configure the Filesystem Disk

Add the public_direct disk to the filesystems.php configuration file in the config directory:

'disks' => [
    // Other disks...

    'public_direct' => [
        'driver' => 'local',
        'root' => public_path(),
        'url' => env('APP_URL'),
        'visibility' => 'public',
    ],
],

Step 2: Set Environment Variable

Ensure your APP_URL environment variable is set correctly in your .env file:

APP_URL=http://your-domain.com

Step 3: Use the Disk in Your Application

You can use this custom disk to store and access files directly in the public directory. Here are some examples:

Storing Files

use Illuminate\Support\Facades\Storage;

// Store a file in the public directory
$filePath = Storage::disk('public_direct')->put('uploads/example.txt', 'File content here');

// Get the URL of the stored file
$fileUrl = Storage::disk('public_direct')->url($filePath);

echo $fileUrl; // Output: http://your-domain.com/uploads/example.txt

Accessing Files

Since files are stored directly in the public directory, you can access them via the URL without any additional slug. For example:

// Assuming the file was stored as 'uploads/example.txt'
$fileUrl = url('uploads/example.txt');

echo $fileUrl; // Output: http://your-domain.com/uploads/example.txt

Benefits

Example Usage in a Controller

Here’s how you might use this in a Laravel controller:

namespace App\Http\Controllers;

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

class FileController extends Controller
{
    public function upload(Request $request)
    {
        if ($request->hasFile('file')) {
            // Store the file directly in the public directory
            $filePath = Storage::disk('public_direct')->put('uploads', $request->file('file'));
            
            // Get the URL of the stored file
            $fileUrl = Storage::disk('public_direct')->url($filePath);

            return response()->json(['url' => $fileUrl]);
        }

        return response()->json(['error' => 'No file uploaded'], 400);
    }
}

In this example, when a file is uploaded, it is stored in the public/uploads directory, and the URL of the file is returned in the response.

Published By: Krishanu Jadiya
Updated at: 2024-08-03 16:03:42

Card Image

Latest JavaScript Features | ES2023 and Beyond

Explore the latest features of JavaScript including ES2023 updates such as Array.findLast(), top-level await, and more. Stay up-to-date with the newest advancements in JavaScript.

Card Image

Understanding JavaScript Hoisting with Examples

Learn about JavaScript hoisting, how it works, and see practical examples demonstrating variable and function hoisting.

Card Image

Dynamic Tabs to Dropdown on Mobile

Convert tabs to dropdown on mobile devices dynamically using JavaScript. Learn how to create a responsive tab system that adapts to different screen sizes.

Card Image

JavaScript Array Functions - Complete Guide

Learn about the most commonly used JavaScript array functions, including map, filter, reduce, forEach, and more. Enhance your coding skills with these essential methods.