1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

LaravelのStorageでプロジェクト外のローカルディスク上にファイルを出力する

Posted at

行うこと

Windows10のxampp上で立ち上げたLaravel8.4.0のプロジェクトで、textファイルをLaravelプロジェクトフォルダ外であるD:/testに出力する。

方法

config/filesystems.phpを編集して、Storageのdiskに新たに保存先を加える。

config/filesystems.php
'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        /* -----付け加え箇所----- */
        'local_test' => [
            'driver' => 'local',
            'root' => 'D:/test',
        ],
        /* ---------- */

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
        ],

    ],

ファイルを出力したいタイミングで記述する

app/Http/Controllers/PostController.php
Storage::disk('local_test')->put($file_name, $file_content);

参考

stack overflow: Adding Custom path for storage in filesystems.php Laravel
LaravelでStorageを使ったファイルおよびディレクトリの操作をまとめてみた
Laravel File Storage(英語)
Laravel File Storage(日本語)

1
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?