Laravel 5.0 Filesystem / Cloud Storage

Introduction

Laravel provides a wonderful filesystem abstraction thanks to the Flysystem PHP package by Frank de Jonge. The Laravel Flysystem integration provides simple to use drivers for working with local filesystems, Amazon S3, and Rackspace Cloud Storage. Even better, it's amazingly simple to switch between these storage options as the API remains the same for each system!

Configuration

The filesystem configuration file is located at config/filesystems.php. Within this file you may configure all of your "disks". Each disk represents a particular storage driver and storage location. Example configurations for each supported driver is included in the configuration file. So, simply modify the configuration to reflect your storage preferences and credentials!

Before using the S3 or Rackspace drivers, you will need to install the appropriate package via Composer:

Of course, you may configure as many disks as you like, and may even have multiple disks that use the same driver.

When using the local driver, note that all file operations are relative to the root directory defined in your configuration file. By default, this value is set to the storage/app directory. Therefore, the following method would store a file in storage/app/file.txt:

Storage::disk('local')->put('file.txt', 'Contents');

Basic Usage

The Storage facade may be used to interact with any of your configured disks. Alternatively, you may type-hint the Illuminate\Contracts\Filesystem\Factory contract on any class that is resolved via the Laravel service container.

Retrieving A Particular Disk

$disk = Storage::disk('s3');

$disk = Storage::disk('local');

Determining If A File Exists

$exists = Storage::disk('s3')->exists('file.jpg');

Calling Methods On The Default Disk

if (Storage::exists('file.jpg'))
{
    //
}

Retrieving A File's Contents

$contents = Storage::get('file.jpg');

Setting A File's Contents

Storage::put('file.jpg', $contents);

Prepend To A File

Storage::prepend('file.log', 'Prepended Text');

Append To A File

Storage::append('file.log', 'Appended Text');

Delete A File

Storage::delete('file.jpg');

Storage::delete(['file1.jpg', 'file2.jpg']);

Copy A File To A New Location

Storage::copy('old/file1.jpg', 'new/file1.jpg');

Move A File To A New Location

Storage::move('old/file1.jpg', 'new/file1.jpg');

Get File Size

$size = Storage::size('file1.jpg');

Get The Last Modification Time (UNIX)

$time = Storage::lastModified('file1.jpg');

Get All Files Within A Directory

$files = Storage::files($directory);

// Recursive...
$files = Storage::allFiles($directory);

Get All Directories Within A Directory

$directories = Storage::directories($directory);

// Recursive...
$directories = Storage::allDirectories($directory);

Create A Directory

Storage::makeDirectory($directory);

Delete A Directory

Storage::deleteDirectory($directory);

Custom Filesystems

Laravel's Flysystem integration provides drivers for several "drivers" out of the box; however, Flysystem is not limited to these and has adapters for many other storage systems. You can create a custom driver if you want to use one of these additional adapters in your Laravel application. Don't worry, it's not too hard!

In order to set up the custom filesystem you will need to create a service provider such as DropboxFilesystemServiceProvider. In the provider's boot method, you can inject an instance of the Illuminate\Contracts\Filesystem\Factory contract and call the extend method of the injected instance. Alternatively, you may use the Disk facade's extend method.

The first argument of the extend method is the name of the driver and the second is a Closure that receives the $app and $config variables. The resolver Closure must return an instance of League\Flysystem\Filesystem.

Note: The $config variable will already contain the values defined in config/filesystems.php for the specified disk.

Dropbox Example

<?php namespace App\Providers;

use Storage;
use League\Flysystem\Filesystem;
use Dropbox\Client as DropboxClient;
use League\Flysystem\Dropbox\DropboxAdapter;
use Illuminate\Support\ServiceProvider;

class DropboxFilesystemServiceProvider extends ServiceProvider {

    public function boot()
    {
        Storage::extend('dropbox', function($app, $config)
        {
            $client = new DropboxClient($config['accessToken'], $config['clientIdentifier']);

            return new Filesystem(new DropboxAdapter($client));
        });
    }

    public function register()
    {
        //
    }

}

ドキュメント章別ページ

Artisan CLI

ヘッダー項目移動

注目:アイコン:ページ内リンク設置(リンクがないヘッダーへの移動では、リンクがある以前のヘッダーのハッシュをURLへ付加します。

移動

クリックで即時移動します。

設定

適用ボタンクリック後に、全項目まとめて適用されます。

カラーテーマ
和文指定 Pagination
和文指定 Scaffold
Largeスクリーン表示幅
インデント
本文フォント
コードフォント
フォント適用確認

フォントの指定フィールドから、フォーカスが外れると、当ブロックの内容に反映されます。EnglishのDisplayもPreviewしてください。

フォント設定時、表示に不具合が出た場合、当サイトのクッキーを削除してください。

バックスラッシュを含むインライン\Code\Blockの例です。

以下はコードブロックの例です。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * ユーザに関連する電話レコードを取得
     */
    public function phone()
    {
        return $this->hasOne('App\Phone');
    }
}

設定を保存する前に、表示が乱れないか必ず確認してください。CSSによるフォントファミリー指定の知識がない場合は、フォントを変更しないほうが良いでしょう。

キーボード・ショートカット

オープン操作

PDC

ページ(章)移動の左オフキャンバスオープン

HA

ヘッダー移動モーダルオープン

MS

移動/設定の右オフキャンバスオープン

ヘッダー移動

T

最初のヘッダーへ移動

E

最後のヘッダーへ移動

NJ

次ヘッダー(H2〜H4)へ移動

BK

前ヘッダー(H2〜H4)へ移動

その他

?

このヘルプページ表示
閉じる