3
3

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】Diskを追加して任意の場所にファイルを保存する

Last updated at Posted at 2020-08-31

やりたいこと

Laarvelが入っているディレクトリより上のディレクトリにファイルを保存したい。例えば /home配下とか。
デフォルトで設定されている local ドライバの root のパスだと、Laravelの storage/app ディレクトリに設定されてしまう。

config/filesystems.phpdisks を追加

config/filesystems.php
'disks' => [

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

        // 追加
        'test' => [
            'driver' => 'local',
            'root' => '/home/import',
        ],

        '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'),
        ],

    ],

新しく test ディスクを追加し、root に任意のパスを設定。Laravelでは同じドライバに対して複数のディスクを持つことが可能。

②ファイルを保存

hoge.php
$file = 'hoge.txt';
$txt = 'hoge';

Storage::disk('test')->put($file, $txt);

config/filesystems.php において、test ドライバで設定した root の値からの相対位置でファイル操作が行われる。つまり上記では、/hoge/import/hoge.txt として保存される。

3
3
0

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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?