記錄一下 Laravel Storage 的常見用法
內容寫入磁盤文件
> php artisan tinker >>> use Illuminate\Support\Facades\Storage; >>> Storage::put('test.txt', 'hello'); => true ls storage/app/ public/ test.txt
文件默認創建在 /storage/app 目錄下
獲取文件存儲的本地磁盤全路徑
繼續在 tinker 中測試一下
>>> storage_path() => "/home/zhongwei/work/my_project/storage" >>> storage_path('test.txt') => "/home/zhongwei/work/my_project/storage/test.txt" >>> storage_path('app/test.txt') => "/home/zhongwei/work/my_project/storage/app/test.txt"
可見,應該是 storage_path('app/test.txt')
刪除文件
>>> Storage::delete('test.txt') => true
文件默認存儲路徑是在哪里設置的
Config/filesystems.php
'disks' => [ 'local' => [ 'driver' => 'local', 'root' => storage_path('app'), ], 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage', 'visibility' => 'public', ],
Local.root 指定的即是默認路徑。
判斷一個文件是否存在
>>> Storage::put('public/test.txt', 'hello'); => true >>> Storage::exists('public/test.txt'); => true >>> Storage::exists('public/test1.txt'); => false