Markus Hedlund
Markus Hedlund

Reputation: 24244

Change storage path in Laravel

Is it possible to change to the storage path? I want it to be outside of the repository in our production environment, for deployments to work properly.

The default path is app/storage/. We are using 4.1.x.

Upvotes: 4

Views: 5704

Answers (4)

Fons
Fons

Reputation: 103

For Laravel 7 through 10, and likely newer versions, too, you can change the config file at app/filesystems.php.

I think the cleanest solution would be to add a disk with a different root, for example

'my_custom_path' => [
    'driver' => 'local',
    'root' => storage_path('path/from/storage/folder'),
],

In this example, 'driver' => 'local' tells the disk is in your project's folder (rather than via FTP, DAV, S3, etc.). and 'root' => ... sets the actual file path. This can be anything, but it's good practice to leave this under the /storage folder in your project, not in any other folder.

After this, you can use code like this to write or retrieve files:

use Illuminate\Support\Facades\Storage;

// Writing "Hello world!" in "file.txt" in your new, custom disk.
Storage::disk('my_custom_path')->put('file.txt', 'Hello world!');

// Retrieving the contents of file.txt in your disk as plain-text.
Storage::disk('my_custom_path')->get('file.txt');

Or you can go to your .env file and change FILESYSTEM_DISK=local to FILESYSTEM_DISK=my_custom_path. If you do this, your new custom disk is set as default and you never need to specify disk('my_custom_path') when using storage, anymore!

Upvotes: 0

A penguin in Redmond
A penguin in Redmond

Reputation: 118

In Laravel 8, it can be set in bootstrap/app.php as follows:

$app->instance('path.storage', '/tmp/laravel_storage');

Based on https://www.amezmo.com/blog/how-to-change-the-default-storage-path-in-laravel/, but there they set it in the register() method of the AppServiceProvider, which appears to be too late. Setting it in bootstrap/app.php, like in jehon's answer, seems to work, though.

Upvotes: 2

jehon
jehon

Reputation: 1648

For Laravel 5.0, you can do that in bootstrap\app.php

$app->useStoragePath("/tmp/laravel_storage/");

Hope this will help someone...

Edit: In Laravel 5.1, this is not enough anymore...

Upvotes: 1

Quasdunk
Quasdunk

Reputation: 15220

Yes, it is. You can define it in bootstrap/paths.php:

/*
|--------------------------------------------------------------------------
| Storage Path
|--------------------------------------------------------------------------
|
| The storage path is used by Laravel to store cached Blade views, logs
| and other pieces of information. You may modify the path here when
| you want to change the location of this directory for your apps.
|
*/

'storage' => __DIR__.'/../app/storage',

Don't forget to set the required read/write permissions for the new directory.

Upvotes: 2

Related Questions