Laravel: Save file to public folder using storage

Configuration

Let’s start with editing config/filesystems.php, and add this entry to ‘disks’ array:

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

Pick the name for disk (public_2) yourself, I’m not very good at naming things, lol.

So the result will look kinda like this:

Usage

<?php

use Illuminate\Support\Facades\Storage;

$file_contents = "Hello!";
Storage::disk('public_2')->put('test_text.txt', $file_contents);

?>

Explanation

Important bits here are ‘root’ parameter and ‘url’ parameter.

‘root’ parameter is local path to desired folder (in this case, /Applications/MAMP/htdocs/laravelproject/public). It’s where the files would be saved or looked for, when using Storage disk parameter.

‘url’ parameter is by-fact base url for our file. In our usage example url generated with Storage::disk(‘public_2’)->url(‘test_text.txt’) will be http://localhost/test_text.txt

Leave a comment

Design a site like this with WordPress.com
Get started