Laravel5.1 文件管理

Laravel提供了一套很好用的文件系統 方便於管理文件夾和文件,支持Amazon S3Rackspace雲存儲等驅動。php

 


1 配置

文件系統的配置文件在 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/ 目錄
        ],

 


2 獲取硬盤實例

要進行文件管理須要那到硬盤實例,咱們能夠經過 Storage 門面的 disk 方法來獲取,以後就能夠進行咱們想要的操做了:app

    public function index()
    {
        $disk = Storage::disk('local');
        // 建立一個文件
        $disk->put('file1.txt', 'Laravel Storage');
    }

 


3 文件操做

3.1 獲取文件

public function index()
    {
        // 取到磁盤實例
        $disk = Storage::disk('local');

        // 取出文件
        $file = $disk->get('test.txt');
        dd($file);
    }

咱們能夠使用get()方法獲取到文件 以字符串的形式傳入文件名就行,可是須要主意:若是你要取到子目錄如下的文件時須要傳入路徑,好比:$disk->get('subpath/.../.../.../file.txt');ide

3.2 判斷文件是否存在

    public function index()
    {
        // 取到磁盤實例
        $disk = Storage::disk('local');

        // 取出文件
        $exists = $disk->exists('image.png');
        dd($exists);    // false
    }

3.3 獲取文件信息

文件大小: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
    }

3.4 儲存文件

    public function index()
    {
        // 取到磁盤實例
        $disk = Storage::disk('local');

        // 儲存文件
        $disk->put('file2.txt', 'file2 content');  // 新建一個文件
    }

3.5 Copy文件

    public function index()
    {
        // 取到磁盤實例
        $disk = Storage::disk('local');

        // 拷貝文件 第一個參數是要拷貝的文件,第二個參數是拷貝到哪裏
        $disk->copy('home/test.txt','rename.txt');
    }

3.6 移動文件

    public function index()
    {
        // 取到磁盤實例
        $disk = Storage::disk('local');

        // 拷貝文件 第一個參數是要移動的文件,第二個參數是移動到哪裏
        $disk->move('file2.txt', 'home/file.txt');
    }

3.7 在文件開頭/結尾添加內容

    public function index()
    {
        // 取到磁盤實例
        $disk = Storage::disk('local');

        // 在文件開頭添加
        $disk->prepend('file1.txt', 'test prepend');
        // 在文件末尾添加
        $disk->append('file1.txt', 'test append');
    }

3.8 刪除文件

    public function index()
    {
        // 取到磁盤實例
        $disk = Storage::disk('local');

        // 刪除單條文件
        $disk->delete('test.txt');
        // 刪除多條文件
        $disk->delete(['test22.txt', 'icon.jpg']);
    }

 


4 目錄操做

4.1 取到目錄下的文件

    public function index()
    {
        // 取到磁盤實例
        $disk = Storage::disk('local');

        $directory = '/';
        // 獲取目錄下的文件
        $files = $disk->files($directory);
        // 獲取目錄下的全部文件(包括子目錄下的文件)
        $allFiles = $disk->allFiles($directory);
        dd($files, $allFiles);
    }

4.2 取到子目錄

    public function index()
    {
        // 取到磁盤實例
        $disk = Storage::disk('local');

        $directory = '/';
        // 獲取目錄下的子目錄
        $directories = $disk->directories($directory);
        // 獲取目錄下的全部子目錄(包括子目錄下的子目錄)
        $allDirectories = $disk->allDirectories($directory);
        dd($directories, $allDirectories);
    }

4.3 建立目錄

    public function index()
    {
        // 取到磁盤實例
        $disk = Storage::disk('local');

        // 建立目錄
        $disk->makeDirectory('test');
        $disk->makeDirectory('test1/test2');
    }

4.4 刪除目錄

    public function index()
    {
        // 取到磁盤實例
        $disk = Storage::disk('local');

        // 刪除目錄
        $disk->deleteDirectory('test');
        $disk->deleteDirectory('test1/test2');
    }
相關文章
相關標籤/搜索