Laravel提供了一套很好用的文件系統 方便於管理文件夾和文件,支持Amazon S3和Rackspace雲存儲等驅動。php
文件系統的配置文件在 config/filesyetems.php 中,且它的註釋寫的很清楚了,此外你能夠在disks數組中建立新的disk:api
<?php return [ /* |-------------------------------------------------------------------------- | Default Filesystem Disk |-------------------------------------------------------------------------- | | Here you may specify the default filesystem disk that should be used | by the framework. A "local" driver, as well as a variety of cloud | based drivers are available for your choosing. Just store away! | | Supported: "local", "ftp", "s3", "rackspace" | */ 'default' => 'local', /* |-------------------------------------------------------------------------- | Default Cloud Filesystem Disk |-------------------------------------------------------------------------- | | Many applications store files both locally and in the cloud. For this | reason, you may specify a default "cloud" driver here. This driver | will be bound as the Cloud disk implementation in the container. | */ 'cloud' => 's3', /* |-------------------------------------------------------------------------- | Filesystem Disks |-------------------------------------------------------------------------- | | Here you may configure as many filesystem "disks" as you wish, and you | may even configure multiple disks of the same driver. Defaults have | been setup for each driver as an example of the required options. | */ 'disks' => [ 'local' => [ 'driver' => 'local', 'root' => storage_path('app'), ], 'ftp' => [ 'driver' => 'ftp', 'host' => 'ftp.example.com', 'username' => 'your-username', 'password' => 'your-password', // Optional FTP Settings... // 'port' => 21, // 'root' => '', // 'passive' => true, // 'ssl' => true, // 'timeout' => 30, ], 's3' => [ 'driver' => 's3', 'key' => 'your-key', 'secret' => 'your-secret', 'region' => 'your-region', 'bucket' => 'your-bucket', ], 'rackspace' => [ 'driver' => 'rackspace', 'username' => 'your-username', 'key' => 'your-key', 'container' => 'your-container', 'endpoint' => 'https://identity.api.rackspacecloud.com/v2.0/', 'region' => 'IAD', 'url_type' => 'publicURL', ], ], ];
通常狀況下最經常使用的是local(本地)存儲,因此特別說下,咱們能夠經過修改'root'來修改咱們的root路徑:數組
'local' => [ 'driver' => 'local', // 'root' => storage_path('app'), 在/storage/app/目錄 'root' => public_path('uploads'), // 在public/uploads/ 目錄 ],
要進行文件管理須要那到硬盤實例,咱們能夠經過 Storage 門面的 disk 方法來獲取,以後就能夠進行咱們想要的操做了:app
public function index() { $disk = Storage::disk('local'); // 建立一個文件 $disk->put('file1.txt', 'Laravel Storage'); }
public function index() { // 取到磁盤實例 $disk = Storage::disk('local'); // 取出文件 $file = $disk->get('test.txt'); dd($file); }
咱們能夠使用get()方法獲取到文件 以字符串的形式傳入文件名就行,可是須要主意:若是你要取到子目錄如下的文件時須要傳入路徑,好比:$disk->get('subpath/.../.../.../file.txt');ide
public function index() { // 取到磁盤實例 $disk = Storage::disk('local'); // 取出文件 $exists = $disk->exists('image.png'); dd($exists); // false }
文件大小:ui
public function index() { // 取到磁盤實例 $disk = Storage::disk('local'); // 取出文件 $size = Storage::size('/home/test.txt'); dd($size); // 4 }
最後修改時間:this
public function index() { // 取到磁盤實例 $disk = Storage::disk('local'); // 取出文件 $time = Storage::lastModified('file1.txt'); // 1507701271 $time = Carbon::createFromTimestamp($time); echo $time; // 2017-10-11 05:54:31 }
public function index() { // 取到磁盤實例 $disk = Storage::disk('local'); // 儲存文件 $disk->put('file2.txt', 'file2 content'); // 新建一個文件 }
public function index() { // 取到磁盤實例 $disk = Storage::disk('local'); // 拷貝文件 第一個參數是要拷貝的文件,第二個參數是拷貝到哪裏 $disk->copy('home/test.txt','rename.txt'); }
public function index() { // 取到磁盤實例 $disk = Storage::disk('local'); // 拷貝文件 第一個參數是要移動的文件,第二個參數是移動到哪裏 $disk->move('file2.txt', 'home/file.txt'); }
public function index() { // 取到磁盤實例 $disk = Storage::disk('local'); // 在文件開頭添加 $disk->prepend('file1.txt', 'test prepend'); // 在文件末尾添加 $disk->append('file1.txt', 'test append'); }
public function index() { // 取到磁盤實例 $disk = Storage::disk('local'); // 刪除單條文件 $disk->delete('test.txt'); // 刪除多條文件 $disk->delete(['test22.txt', 'icon.jpg']); }
public function index() { // 取到磁盤實例 $disk = Storage::disk('local'); $directory = '/'; // 獲取目錄下的文件 $files = $disk->files($directory); // 獲取目錄下的全部文件(包括子目錄下的文件) $allFiles = $disk->allFiles($directory); dd($files, $allFiles); }
public function index() { // 取到磁盤實例 $disk = Storage::disk('local'); $directory = '/'; // 獲取目錄下的子目錄 $directories = $disk->directories($directory); // 獲取目錄下的全部子目錄(包括子目錄下的子目錄) $allDirectories = $disk->allDirectories($directory); dd($directories, $allDirectories); }
public function index() { // 取到磁盤實例 $disk = Storage::disk('local'); // 建立目錄 $disk->makeDirectory('test'); $disk->makeDirectory('test1/test2'); }
public function index() { // 取到磁盤實例 $disk = Storage::disk('local'); // 刪除目錄 $disk->deleteDirectory('test'); $disk->deleteDirectory('test1/test2'); }