Laravel 的文件存儲 - Storage


記錄一下 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM