檔案儲存
簡介
Laravel 藉由 Frank de Jonge 撰寫的優秀 Flysystem PHP 套件,提供了強大的檔案系統抽象化功能。Laravel Flysystem 整合提供了簡單的驅動程式,用於處理本機檔案系統、SFTP 和 Amazon S3。更棒的是,在您的本機開發機器和生產伺服器之間切換這些儲存選項非常簡單,因為每個系統的 API 都保持不變。
設定
Laravel 的檔案系統設定檔位於 config/filesystems.php
。在此檔案中,您可以設定所有檔案系統「磁碟」。每個磁碟代表特定的儲存驅動程式和儲存位置。設定檔中包含每個支援驅動程式的範例設定,您可以修改設定以反映您的儲存偏好和憑證。
local
驅動程式與儲存在執行 Laravel 應用程式伺服器本機上的檔案互動,而 s3
驅動程式則用於寫入 Amazon 的 S3 雲端儲存服務。
您可以設定任意數量的磁碟,甚至可以有多個使用相同驅動程式的磁碟。
本地驅動程式
當使用 local
驅動程式時,所有檔案操作都相對於您的 filesystems
設定檔中定義的 root
目錄。預設情況下,此值設定為 storage/app/private
目錄。因此,以下方法將寫入 storage/app/private/example.txt
1use Illuminate\Support\Facades\Storage;2 3Storage::disk('local')->put('example.txt', 'Contents');
公開磁碟
您的應用程式 filesystems
設定檔中包含的 public
磁碟,旨在用於將公開存取的檔案。預設情況下,public
磁碟使用 local
驅動程式,並將其檔案儲存在 storage/app/public
中。
如果您的 public
磁碟使用 local
驅動程式,並且您想要從網頁存取這些檔案,您應該建立從來源目錄 storage/app/public
到目標目錄 public/storage
的符號連結
若要建立符號連結,您可以使用 storage:link
Artisan 命令
1php artisan storage:link
檔案儲存完成且符號連結建立後,您可以使用 asset
輔助函式建立檔案的 URL
1echo asset('storage/file.txt');
您可以在 filesystems
設定檔中設定其他符號連結。當您執行 storage:link
命令時,將會建立每個已設定的連結
1'links' => [2 public_path('storage') => storage_path('app/public'),3 public_path('images') => storage_path('app/images'),4],
storage:unlink
命令可用於銷毀您設定的符號連結
1php artisan storage:unlink
驅動程式先決條件
S3 驅動程式設定
在使用 S3 驅動程式之前,您需要透過 Composer 套件管理器安裝 Flysystem S3 套件
1composer require league/flysystem-aws-s3-v3 "^3.0" --with-all-dependencies
S3 磁碟設定陣列位於您的 config/filesystems.php
設定檔中。通常,您應該使用以下環境變數設定您的 S3 資訊和憑證,這些變數由 config/filesystems.php
設定檔引用
1AWS_ACCESS_KEY_ID=<your-key-id>2AWS_SECRET_ACCESS_KEY=<your-secret-access-key>3AWS_DEFAULT_REGION=us-east-14AWS_BUCKET=<your-bucket-name>5AWS_USE_PATH_STYLE_ENDPOINT=false
為了方便起見,這些環境變數符合 AWS CLI 使用的命名慣例。
FTP 驅動程式設定
在使用 FTP 驅動程式之前,您需要透過 Composer 套件管理器安裝 Flysystem FTP 套件
1composer require league/flysystem-ftp "^3.0"
Laravel 的 Flysystem 整合與 FTP 完美搭配;但是,框架的預設 config/filesystems.php
設定檔中未包含範例設定。如果您需要設定 FTP 檔案系統,可以使用以下設定範例
1'ftp' => [ 2 'driver' => 'ftp', 3 'host' => env('FTP_HOST'), 4 'username' => env('FTP_USERNAME'), 5 'password' => env('FTP_PASSWORD'), 6 7 // Optional FTP Settings... 8 // 'port' => env('FTP_PORT', 21), 9 // 'root' => env('FTP_ROOT'),10 // 'passive' => true,11 // 'ssl' => true,12 // 'timeout' => 30,13],
SFTP 驅動程式設定
在使用 SFTP 驅動程式之前,您需要透過 Composer 套件管理器安裝 Flysystem SFTP 套件
1composer require league/flysystem-sftp-v3 "^3.0"
Laravel 的 Flysystem 整合與 SFTP 完美搭配;但是,框架的預設 config/filesystems.php
設定檔中未包含範例設定。如果您需要設定 SFTP 檔案系統,可以使用以下設定範例
1'sftp' => [ 2 'driver' => 'sftp', 3 'host' => env('SFTP_HOST'), 4 5 // Settings for basic authentication... 6 'username' => env('SFTP_USERNAME'), 7 'password' => env('SFTP_PASSWORD'), 8 9 // Settings for SSH key based authentication with encryption password...10 'privateKey' => env('SFTP_PRIVATE_KEY'),11 'passphrase' => env('SFTP_PASSPHRASE'),12 13 // Settings for file / directory permissions...14 'visibility' => 'private', // `private` = 0600, `public` = 064415 'directory_visibility' => 'private', // `private` = 0700, `public` = 075516 17 // Optional SFTP Settings...18 // 'hostFingerprint' => env('SFTP_HOST_FINGERPRINT'),19 // 'maxTries' => 4,20 // 'passphrase' => env('SFTP_PASSPHRASE'),21 // 'port' => env('SFTP_PORT', 22),22 // 'root' => env('SFTP_ROOT', ''),23 // 'timeout' => 30,24 // 'useAgent' => true,25],
範圍限定和唯讀檔案系統
範圍限定磁碟允許您定義檔案系統,其中所有路徑都會自動以給定的路徑前綴開頭。在建立範圍限定檔案系統磁碟之前,您需要透過 Composer 套件管理器安裝額外的 Flysystem 套件
1composer require league/flysystem-path-prefixing "^3.0"
您可以透過定義使用 scoped
驅動程式的磁碟,來建立任何現有檔案系統磁碟的路徑範圍限定實例。例如,您可以建立一個磁碟,將您現有的 s3
磁碟範圍限定為特定的路徑前綴,然後每次使用您的範圍限定磁碟進行檔案操作時,都將使用指定的前綴
1's3-videos' => [2 'driver' => 'scoped',3 'disk' => 's3',4 'prefix' => 'path/to/videos',5],
「唯讀」磁碟允許您建立不允許寫入操作的檔案系統磁碟。在使用 read-only
設定選項之前,您需要透過 Composer 套件管理器安裝額外的 Flysystem 套件
1composer require league/flysystem-read-only "^3.0"
接下來,您可以將 read-only
設定選項包含在一個或多個磁碟的設定陣列中
1's3-videos' => [2 'driver' => 's3',3 // ...4 'read-only' => true,5],
Amazon S3 相容檔案系統
預設情況下,您的應用程式 filesystems
設定檔包含 s3
磁碟的磁碟設定。除了使用此磁碟與 Amazon S3 互動外,您還可以使用它與任何 S3 相容的檔案儲存服務互動,例如 MinIO、DigitalOcean Spaces、Vultr Object Storage、Cloudflare R2 或 Hetzner Cloud Storage。
通常,在更新磁碟的憑證以符合您計劃使用的服務憑證後,您只需要更新 endpoint
設定選項的值。此選項的值通常透過 AWS_ENDPOINT
環境變數定義
1'endpoint' => env('AWS_ENDPOINT', 'https://minio:9000'),
MinIO
為了讓 Laravel 的 Flysystem 整合在使用 MinIO 時產生正確的 URL,您應該定義 AWS_URL
環境變數,使其與您應用程式的本機 URL 相符,並在 URL 路徑中包含存储桶名稱
1AWS_URL=https://127.0.0.1:9000/local
如果用戶端無法存取 endpoint
,則透過 temporaryUrl
方法產生暫時儲存 URL 可能在使用 MinIO 時無法運作。
取得磁碟實例
可以使用 Storage
外觀模式與任何已設定的磁碟互動。例如,您可以使用外觀模式上的 put
方法將頭像儲存在預設磁碟上。如果您在未先呼叫 disk
方法的情況下呼叫 Storage
外觀模式上的方法,則該方法將自動傳遞到預設磁碟
1use Illuminate\Support\Facades\Storage;2 3Storage::put('avatars/1', $content);
如果您的應用程式與多個磁碟互動,您可以使用 Storage
外觀模式上的 disk
方法來處理特定磁碟上的檔案
1Storage::disk('s3')->put('avatars/1', $content);
隨需磁碟
有時您可能希望在執行階段使用給定的設定建立磁碟,而該設定實際上並未出現在應用程式的 filesystems
設定檔中。為了實現這一點,您可以將設定陣列傳遞給 Storage
外觀模式的 build
方法
1use Illuminate\Support\Facades\Storage;2 3$disk = Storage::build([4 'driver' => 'local',5 'root' => '/path/to/root',6]);7 8$disk->put('image.jpg', $content);
檢索檔案
get
方法可用於檢索檔案的內容。該方法將傳回檔案的原始字串內容。請記住,所有檔案路徑都應相對於磁碟的「根」位置指定
1$contents = Storage::get('file.jpg');
如果您檢索的檔案包含 JSON,您可以使用 json
方法來檢索檔案並解碼其內容
1$orders = Storage::json('orders.json');
exists
方法可用於判斷檔案是否存在於磁碟上
1if (Storage::disk('s3')->exists('file.jpg')) {2 // ...3}
missing
方法可用於判斷檔案是否從磁碟中遺失
1if (Storage::disk('s3')->missing('file.jpg')) {2 // ...3}
下載檔案
download
方法可用於產生強制使用者瀏覽器下載指定路徑檔案的回應。download
方法接受檔名作為方法的第二個引數,這將決定下載檔案的使用者看到的檔名。最後,您可以將 HTTP 標頭陣列作為方法的第三個引數傳遞
1return Storage::download('file.jpg');2 3return Storage::download('file.jpg', $name, $headers);
檔案 URL
您可以使用 url
方法取得給定檔案的 URL。如果您使用的是 local
驅動程式,這通常只會在給定的路徑前面加上 /storage
,並傳回檔案的相對 URL。如果您使用的是 s3
驅動程式,則會傳回完整合格的遠端 URL
1use Illuminate\Support\Facades\Storage;2 3$url = Storage::url('file.jpg');
當使用 local
驅動程式時,所有應公開存取的檔案都應放置在 storage/app/public
目錄中。此外,您應該建立符號連結,在 public/storage
指向 storage/app/public
目錄。
當使用 local
驅動程式時,url
的傳回值不是 URL 編碼的。因此,我們建議始終使用將建立有效 URL 的名稱儲存您的檔案。
URL 主機自訂
如果您想修改使用 Storage
外觀模式產生的 URL 的主機,您可以在磁碟的設定陣列中新增或變更 url
選項
1'public' => [2 'driver' => 'local',3 'root' => storage_path('app/public'),4 'url' => env('APP_URL').'/storage',5 'visibility' => 'public',6 'throw' => false,7],
暫時 URL
使用 temporaryUrl
方法,您可以為使用 local
和 s3
驅動程式儲存的檔案建立暫時 URL。此方法接受路徑和 DateTime
實例,指定 URL 應何時過期
1use Illuminate\Support\Facades\Storage;2 3$url = Storage::temporaryUrl(4 'file.jpg', now()->addMinutes(5)5);
啟用本機暫時 URL
如果您在 local
驅動程式引入暫時 URL 支援之前開始開發您的應用程式,您可能需要啟用本機暫時 URL。若要執行此操作,請將 serve
選項新增至 config/filesystems.php
設定檔中 local
磁碟的設定陣列
1'local' => [2 'driver' => 'local',3 'root' => storage_path('app/private'),4 'serve' => true, 5 'throw' => false,6],
S3 請求參數
如果您需要指定額外的 S3 請求參數,您可以將請求參數陣列作為第三個參數傳遞給 temporaryUrl
方法
1$url = Storage::temporaryUrl(2 'file.jpg',3 now()->addMinutes(5),4 [5 'ResponseContentType' => 'application/octet-stream',6 'ResponseContentDisposition' => 'attachment; filename=file2.jpg',7 ]8);
自訂暫時網址
如果您需要自訂特定儲存磁碟的暫時網址建立方式,您可以使用 buildTemporaryUrlsUsing
方法。例如,當您的控制器允許您下載透過通常不支援暫時網址的磁碟所儲存的檔案時,這會很有用。通常,此方法應從服務提供者的 boot
方法中呼叫
1<?php 2 3namespace App\Providers; 4 5use DateTime; 6use Illuminate\Support\Facades\Storage; 7use Illuminate\Support\Facades\URL; 8use Illuminate\Support\ServiceProvider; 9 10class AppServiceProvider extends ServiceProvider11{12 /**13 * Bootstrap any application services.14 */15 public function boot(): void16 {17 Storage::disk('local')->buildTemporaryUrlsUsing(18 function (string $path, DateTime $expiration, array $options) {19 return URL::temporarySignedRoute(20 'files.download',21 $expiration,22 array_merge($options, ['path' => $path])23 );24 }25 );26 }27}
暫時上傳網址
只有 s3
驅動程式支援產生暫時上傳網址的功能。
如果您需要產生一個可用於從您的用戶端應用程式直接上傳檔案的暫時網址,您可以使用 temporaryUploadUrl
方法。此方法接受一個路徑和一個 DateTime
實例,用於指定網址應何時過期。temporaryUploadUrl
方法會傳回一個關聯陣列,可以將其解構成上傳網址和應包含在上傳請求中的標頭
1use Illuminate\Support\Facades\Storage;2 3['url' => $url, 'headers' => $headers] = Storage::temporaryUploadUrl(4 'file.jpg', now()->addMinutes(5)5);
此方法主要適用於需要用戶端應用程式直接將檔案上傳到雲端儲存系統 (例如 Amazon S3) 的無伺服器環境。
檔案元數據
除了讀取和寫入檔案之外,Laravel 也可以提供關於檔案本身的資訊。例如,size
方法可用於取得檔案大小 (以位元組為單位)
1use Illuminate\Support\Facades\Storage;2 3$size = Storage::size('file.jpg');
lastModified
方法會傳回檔案上次修改時間的 UNIX 時間戳記
1$time = Storage::lastModified('file.jpg');
給定檔案的 MIME 類型可以透過 mimeType
方法取得
1$mime = Storage::mimeType('file.jpg');
檔案路徑
您可以使用 path
方法來取得給定檔案的路徑。如果您使用的是 local
驅動程式,這將傳回檔案的絕對路徑。如果您使用的是 s3
驅動程式,此方法將傳回檔案在 S3 儲存桶中的相對路徑
1use Illuminate\Support\Facades\Storage;2 3$path = Storage::path('file.jpg');
儲存檔案
put
方法可用於將檔案內容儲存到磁碟上。您也可以將 PHP resource
傳遞給 put
方法,這將使用 Flysystem 的底層串流支援。請記住,所有檔案路徑都應相對於為磁碟設定的「根」位置指定
1use Illuminate\Support\Facades\Storage;2 3Storage::put('file.jpg', $contents);4 5Storage::put('file.jpg', $resource);
寫入失敗
如果 put
方法 (或其他「寫入」操作) 無法將檔案寫入磁碟,則會傳回 false
1if (! Storage::put('file.jpg', $contents)) {2 // The file could not be written to disk...3}
如果您願意,您可以在檔案系統磁碟的組態陣列中定義 throw
選項。當此選項定義為 true
時,「寫入」方法 (例如 put
) 將在寫入操作失敗時擲回 League\Flysystem\UnableToWriteFile
的實例
1'public' => [2 'driver' => 'local',3 // ...4 'throw' => true,5],
前置和附加到檔案
prepend
和 append
方法允許您寫入檔案的開頭或結尾
1Storage::prepend('file.log', 'Prepended Text');2 3Storage::append('file.log', 'Appended Text');
複製和移動檔案
copy
方法可用於將現有檔案複製到磁碟上的新位置,而 move
方法可用於重新命名或將現有檔案移動到新位置
1Storage::copy('old/file.jpg', 'new/file.jpg');2 3Storage::move('old/file.jpg', 'new/file.jpg');
自動串流
將檔案串流到儲存空間可顯著降低記憶體使用量。如果您希望 Laravel 自動管理將給定檔案串流到您的儲存位置,您可以使用 putFile
或 putFileAs
方法。此方法接受 Illuminate\Http\File
或 Illuminate\Http\UploadedFile
實例,並會自動將檔案串流到您想要的位置
1use Illuminate\Http\File;2use Illuminate\Support\Facades\Storage;3 4// Automatically generate a unique ID for filename...5$path = Storage::putFile('photos', new File('/path/to/photo'));6 7// Manually specify a filename...8$path = Storage::putFileAs('photos', new File('/path/to/photo'), 'photo.jpg');
關於 putFile
方法,有幾點重要的注意事項。請注意,我們僅指定了目錄名稱,而未指定檔案名稱。預設情況下,putFile
方法將產生一個唯一 ID 作為檔案名稱。檔案的副檔名將透過檢查檔案的 MIME 類型來決定。檔案的路徑將由 putFile
方法傳回,以便您可以將路徑 (包括產生的檔案名稱) 儲存在您的資料庫中。
putFile
和 putFileAs
方法也接受一個參數,用於指定儲存檔案的「可見性」。如果您將檔案儲存在雲端磁碟 (例如 Amazon S3) 上,並希望透過產生的網址公開存取該檔案,這會特別有用
1Storage::putFile('photos', new File('/path/to/photo'), 'public');
檔案上傳
在 Web 應用程式中,儲存檔案最常見的用例之一是儲存使用者上傳的檔案,例如照片和文件。Laravel 讓使用上傳檔案實例上的 store
方法來儲存上傳的檔案變得非常容易。使用您希望儲存上傳檔案的路徑呼叫 store
方法
1<?php 2 3namespace App\Http\Controllers; 4 5use App\Http\Controllers\Controller; 6use Illuminate\Http\Request; 7 8class UserAvatarController extends Controller 9{10 /**11 * Update the avatar for the user.12 */13 public function update(Request $request): string14 {15 $path = $request->file('avatar')->store('avatars');16 17 return $path;18 }19}
關於此範例,有幾點重要的注意事項。請注意,我們僅指定了目錄名稱,而未指定檔案名稱。預設情況下,store
方法將產生一個唯一 ID 作為檔案名稱。檔案的副檔名將透過檢查檔案的 MIME 類型來決定。檔案的路徑將由 store
方法傳回,以便您可以將路徑 (包括產生的檔案名稱) 儲存在您的資料庫中。
您也可以在 Storage
facade 上呼叫 putFile
方法,以執行與上述範例相同的檔案儲存操作
1$path = Storage::putFile('avatars', $request->file('avatar'));
指定檔案名稱
如果您不希望自動為您儲存的檔案指派檔案名稱,您可以使用 storeAs
方法,該方法接收路徑、檔案名稱和 (選用的) 磁碟作為其引數
1$path = $request->file('avatar')->storeAs(2 'avatars', $request->user()->id3);
您也可以在 Storage
facade 上使用 putFileAs
方法,這將執行與上述範例相同的檔案儲存操作
1$path = Storage::putFileAs(2 'avatars', $request->file('avatar'), $request->user()->id3);
不可列印和無效的 Unicode 字元將會自動從檔案路徑中移除。因此,您可能希望在將檔案路徑傳遞給 Laravel 的檔案儲存方法之前先清理它們。檔案路徑會使用 League\Flysystem\WhitespacePathNormalizer::normalizePath
方法進行正規化。
指定磁碟
預設情況下,此上傳檔案的 store
方法將使用您的預設磁碟。如果您想要指定另一個磁碟,請將磁碟名稱作為第二個引數傳遞給 store
方法
1$path = $request->file('avatar')->store(2 'avatars/'.$request->user()->id, 's3'3);
如果您使用的是 storeAs
方法,您可以將磁碟名稱作為第三個引數傳遞給該方法
1$path = $request->file('avatar')->storeAs(2 'avatars',3 $request->user()->id,4 's3'5);
其他上傳檔案資訊
如果您想要取得上傳檔案的原始名稱和副檔名,您可以使用 getClientOriginalName
和 getClientOriginalExtension
方法來執行此操作
1$file = $request->file('avatar');2 3$name = $file->getClientOriginalName();4$extension = $file->getClientOriginalExtension();
但是,請記住,getClientOriginalName
和 getClientOriginalExtension
方法被認為是不安全的,因為檔案名稱和副檔名可能會被惡意使用者竄改。因此,您通常應該偏好使用 hashName
和 extension
方法來取得給定檔案上傳的名稱和副檔名
1$file = $request->file('avatar');2 3$name = $file->hashName(); // Generate a unique, random name...4$extension = $file->extension(); // Determine the file's extension based on the file's MIME type...
檔案可見性
在 Laravel 的 Flysystem 整合中,「可見性」是跨多個平台檔案權限的抽象概念。檔案可以宣告為 public
或 private
。當檔案宣告為 public
時,表示您指出該檔案通常應該可供其他人存取。例如,當使用 S3 驅動程式時,您可以檢索 public
檔案的網址。
您可以在透過 put
方法寫入檔案時設定可見性
1use Illuminate\Support\Facades\Storage;2 3Storage::put('file.jpg', $contents, 'public');
如果檔案已經儲存,則可以使用 getVisibility
和 setVisibility
方法來檢索和設定其可見性
1$visibility = Storage::getVisibility('file.jpg');2 3Storage::setVisibility('file.jpg', 'public');
當與上傳檔案互動時,您可以使用 storePublicly
和 storePubliclyAs
方法來儲存具有 public
可見性的上傳檔案
1$path = $request->file('avatar')->storePublicly('avatars', 's3');2 3$path = $request->file('avatar')->storePubliclyAs(4 'avatars',5 $request->user()->id,6 's3'7);
本機檔案和可見性
當使用 local
驅動程式時,public
可見性 會轉譯為目錄的 0755
權限和檔案的 0644
權限。您可以在應用程式的 filesystems
組態檔中修改權限對應
1 2``````php 3'local' => [ 4 'driver' => 'local', 5 'root' => storage_path('app'), 6 'permissions' => [ 7 'file' => [ 8 'public' => 0644, 9 'private' => 0600,10 ],11 'dir' => [12 'public' => 0755,13 'private' => 0700,14 ],15 ],16 'throw' => false,17],
刪除檔案
delete
方法接受單一檔案名稱或要刪除的檔案陣列
1 2``````php3use Illuminate\Support\Facades\Storage;4 5Storage::delete('file.jpg');6 7Storage::delete(['file.jpg', 'file2.jpg']);
如有必要,您可以指定應從哪個磁碟刪除檔案
1 2``````php3use Illuminate\Support\Facades\Storage;4 5Storage::disk('s3')->delete('path/file.jpg');
目錄
取得目錄中的所有檔案
files
方法會傳回給定目錄中所有檔案的陣列。如果您想要檢索給定目錄中 (包括所有子目錄) 的所有檔案清單,您可以使用 allFiles
方法
1use Illuminate\Support\Facades\Storage;2 3$files = Storage::files($directory);4 5$files = Storage::allFiles($directory);
取得目錄中的所有子目錄
directories
方法會傳回給定目錄中所有子目錄的陣列。此外,您可以使用 allDirectories
方法來取得給定目錄及其所有子目錄中所有子目錄的清單
1$directories = Storage::directories($directory);2 3$directories = Storage::allDirectories($directory);
建立目錄
makeDirectory
方法將建立給定的目錄,包括任何需要的子目錄
1Storage::makeDirectory($directory);
刪除目錄
最後,deleteDirectory
方法可用於移除目錄及其所有檔案
1Storage::deleteDirectory($directory);
測試
Storage
facade 的 fake
方法可讓您輕鬆產生一個虛擬磁碟,結合 Illuminate\Http\UploadedFile
類別的檔案產生公用程式,可大幅簡化檔案上傳的測試。例如
1<?php 2 3use Illuminate\Http\UploadedFile; 4use Illuminate\Support\Facades\Storage; 5 6test('albums can be uploaded', function () { 7 Storage::fake('photos'); 8 9 $response = $this->json('POST', '/photos', [10 UploadedFile::fake()->image('photo1.jpg'),11 UploadedFile::fake()->image('photo2.jpg')12 ]);13 14 // Assert one or more files were stored...15 Storage::disk('photos')->assertExists('photo1.jpg');16 Storage::disk('photos')->assertExists(['photo1.jpg', 'photo2.jpg']);17 18 // Assert one or more files were not stored...19 Storage::disk('photos')->assertMissing('missing.jpg');20 Storage::disk('photos')->assertMissing(['missing.jpg', 'non-existing.jpg']);21 22 // Assert that the number of files in a given directory matches the expected count...23 Storage::disk('photos')->assertCount('/wallpapers', 2);24 25 // Assert that a given directory is empty...26 Storage::disk('photos')->assertDirectoryEmpty('/wallpapers');27});
1<?php 2 3namespace Tests\Feature; 4 5use Illuminate\Http\UploadedFile; 6use Illuminate\Support\Facades\Storage; 7use Tests\TestCase; 8 9class ExampleTest extends TestCase10{11 public function test_albums_can_be_uploaded(): void12 {13 Storage::fake('photos');14 15 $response = $this->json('POST', '/photos', [16 UploadedFile::fake()->image('photo1.jpg'),17 UploadedFile::fake()->image('photo2.jpg')18 ]);19 20 // Assert one or more files were stored...21 Storage::disk('photos')->assertExists('photo1.jpg');22 Storage::disk('photos')->assertExists(['photo1.jpg', 'photo2.jpg']);23 24 // Assert one or more files were not stored...25 Storage::disk('photos')->assertMissing('missing.jpg');26 Storage::disk('photos')->assertMissing(['missing.jpg', 'non-existing.jpg']);27 28 // Assert that the number of files in a given directory matches the expected count...29 Storage::disk('photos')->assertCount('/wallpapers', 2);30 31 // Assert that a given directory is empty...32 Storage::disk('photos')->assertDirectoryEmpty('/wallpapers');33 }34}
預設情況下,fake
方法將刪除其暫存目錄中的所有檔案。如果您想要保留這些檔案,您可以改用「persistentFake」方法。如需有關測試檔案上傳的更多資訊,您可以查閱 HTTP 測試文件中有關檔案上傳的資訊。
image
方法需要 GD 擴充功能。
自訂檔案系統
Laravel 的 Flysystem 整合開箱即用即支援多個「驅動程式」;但是,Flysystem 不僅限於這些驅動程式,並且具有適用於許多其他儲存系統的轉接器。如果您想在您的 Laravel 應用程式中使用其中一個額外的轉接器,您可以建立自訂驅動程式。
為了定義自訂檔案系統,您將需要一個 Flysystem 轉接器。讓我們將社群維護的 Dropbox 轉接器新增到我們的專案中
1composer require spatie/flysystem-dropbox
接下來,您可以在應用程式的 服務提供者 之一的 boot
方法中註冊驅動程式。為了完成此操作,您應該使用 Storage
facade 的 extend
方法
1<?php 2 3namespace App\Providers; 4 5use Illuminate\Contracts\Foundation\Application; 6use Illuminate\Filesystem\FilesystemAdapter; 7use Illuminate\Support\Facades\Storage; 8use Illuminate\Support\ServiceProvider; 9use League\Flysystem\Filesystem;10use Spatie\Dropbox\Client as DropboxClient;11use Spatie\FlysystemDropbox\DropboxAdapter;12 13class AppServiceProvider extends ServiceProvider14{15 /**16 * Register any application services.17 */18 public function register(): void19 {20 // ...21 }22 23 /**24 * Bootstrap any application services.25 */26 public function boot(): void27 {28 Storage::extend('dropbox', function (Application $app, array $config) {29 $adapter = new DropboxAdapter(new DropboxClient(30 $config['authorization_token']31 ));32 33 return new FilesystemAdapter(34 new Filesystem($adapter, $config),35 $adapter,36 $config37 );38 });39 }40}
extend
方法的第一個引數是驅動程式的名稱,第二個引數是接收 $app
和 $config
變數的閉包。閉包必須傳回 Illuminate\Filesystem\FilesystemAdapter
的實例。$config
變數包含在 config/filesystems.php
中為指定磁碟定義的值。
一旦您建立並註冊了擴充功能的服務提供者,您就可以在您的 config/filesystems.php
組態檔中使用 dropbox
驅動程式。