[Laravel5.2 documentation] service-file system/cloud storage

Source: Internet
Author: User
[Laravel5.2 documentation] service-file system cloud storage 1. Introduction

Laravel's PHP package Flysystem developed based on Frank de Jonge provides powerful file system abstraction. Laravel file system integration provides simple use of drivers for processing local file systems, including Amazon S3 and Rackspace cloud storage. In addition, switching between these storage options is very simple, because the API is the same for each system.

2. configuration

The file system configuration file is located in config/filesystems. php. All "hard disks" can be configured in this file. each hard disk describes the specific storage drive and storage location. The sample configuration for each supported driver is included in this configuration file. Therefore, you can edit the configuration to reflect your storage parameters and authentication information.

Of course, if you want to configure the disk quantity, you can configure the disk quantity. multiple disks can also share the same drive.

Local driver

When using the local driver, note that all file operations are relative to the root directory defined in the configuration file. by default, this value is set to the storage/app Directory. therefore, the following method stores the file to storage/app/file.txt:

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

Other driving preparations

Before using the Amazon S3 or Rackspace driver, you need to install the corresponding package through Composer:

  • Amazon S3: leleague/flysystem-aws-s3-v3 ~ 1.0
  • Rackspace: leleague/flysystem-rackspace ~ 1.0
3. Basic use3.1 obtain a hard disk instance

The Storage facade is used to interact with all the disks you configured. for example, you can use the put method on the facade to store the avatar to the default disk, if you call the method on the Storage facade but call the disk method first, this method is automatically transferred to the default disk:

 Id, file_get_contents ($ request-> file ('Avatar ')-> getRealPath ()));}}

When multiple disks are used, you can use the disk method on the Storage facade to access a specific disk. Of course, you can continue to use the method chain to execute the method on the disk:

$disk = Storage::disk('s3');$contents = Storage::disk('local')->get('file.jpg')
3.2 getting files

The get method is used to obtain the content of a given object. this method returns the native string content of the object:

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

The exists method is used to determine whether a specified file exists on the disk:

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

File metadata

The size method returns the file size in bytes:

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

The lastModified method returns the last modification time of the file in UNIX timestamp format:

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

The put method is used to store files to disks. You can pass a PHP resource to the put method, which uses the stream support at the bottom layer of Flysystem. We recommend that you use file streams when processing large files:

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

The copy method copies existing files in the disk from one place to another:

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

The move method moves the existing files in the disk from a certain place to another place:

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

Add content to the beginning/end of a file

The prepend and append methods allow you to easily insert content to the beginning/end of a file:

Storage::prepend('file.log', 'Prepended Text');Storage::append('file.log', 'Appended Text');
3.4 Delete an object

The delete method receives a single file name or multiple file arrays and removes them from the disk:

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

Obtains all files in a directory.

The files method returns an array of all files in a given directory. if you want to obtain a list of all files containing subdirectories in a given directory, you can use the allFiles method:

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

Obtain all subdirectories in a directory

The directories method returns the array of all directories in the given directory. In addition, you can use the allDirectories method to obtain the arrays of all nested subdirectories:

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

Create directory

The makeDirectory method creates a given directory that contains sub-directories (recursion ):

Storage::makeDirectory($directory);

Delete directory

Finally, the deleteDirectory method is used to remove a directory, including all files in the directory:

Storage::deleteDirectory($directory);
4. Custom file system

Laravel's Flysystem integration supports custom drivers. to set a custom file system, you need to create a service provider such as DropboxServiceProvider. In the provider's boot method, you can use the extend method of the Storage facade to define a custom driver:

     

The first parameter of the extend method is the driver name, and the second parameter is the closure for getting the $ app and $ config variables. The parser closure must return a League \ Flysystem \ Filesystem instance. The $ config variable contains the options defined in the config/filesystems. php configuration file for a specific disk.

After creating a service provider for registration extension, you can use the dropbox driver in the config/filesystem. php configuration file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.