Basic laravel tutorial-file system/cloud storage overview
Laravel provides a powerful file system abstraction, thanks to the Flyststem PHP package developed by Frank de Jonge. Laravel's file system provides support for some storage drivers, including local file systems, Amazon S3, and Rackspace cloud storage. What's more, it can switch these storage systems through the storage configuration option, because laravel provides a unified API interface for them.
Configuration
The configuration options of the file system are stored in the config/filesystems. php file. You can configure all disks in this file. Each disk option contains the storage driver used and its storage location. For storage drivers supported by laravel by default, corresponding configuration examples are provided in this file. Therefore, you can simply modify the configuration options in this file to use its powerful functions.
Of course, you can configure multiple disks, or even make multiple disks use the same driver.
Public disk
Public disk means it can be publicly accessed. By default, the local drive is used for public disks, and the file storage location is in the storage/app/public directory. If you want files in this directory to be accessible on the web, you need to create a symbolic link from public/storage to storage/app/public. This convention keeps publicly accessible files in the same directory and allows you to easily share the entire deployment process when using a painless, continuous deployment system such as Envoyer.
Of course, once the file is stored and a symbolic link is established. You can use the asset help method to generate the file URL:
echo asset('storage/file.txt')
Local driver
When using the local driver, you need to know that all file operations are relative to the directory defined by the root option in the configuration file. The default value is the storage/app Directory. Therefore, the following method stores the file to storage/app/file.txt:
Storage::disk('local')->put('file.txt', 'Contents');
Prerequisites for other drivers
Before using the S3 or Rackspace driver, you need to install the appropriate package file through Composer:
- Amazon S3: leleague/flysystem-aws-s3-v3 ~ 1.0
- Rackspace: leleague/flysystem-rackspace ~ 1.0
FTP driver configuration
Laravel's file system supports FTP integration, but the default configuration file does not provide an example. If you need to configure the FTP file system, you can use the following configuration example:
'ftp' => [ 'dirver' => 'ftp', 'host' => 'ftp.example.com', 'username' => 'your-username', 'password' => 'your-password', // Optional FTP Settings... // 'port' => 21, // 'root' => '', // 'passive' => true, // 'ssl' => true, // 'timeout' => 30,],
Rackspace driver configuration
Laravel's file system supports Rackspace integration, but the default configuration file does not provide an example. If you need to configure the Rackspace file system, you can use the following example:
'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',],
Basic usage
Storage faces can be used to interact with the disk you configured. For example, you can use the put method to store your profile picture to the default disk. If you do not use the disk method before calling this method, this method will automatically pass the avatar to the default disk:
id, file_get_contents($request->file('avatar')->getRealPath()) ); }}
When you use multiple disks, you can use the disk method of the Storage mask to specify the disk to be accessed. Of course, you can use the chained method for continuous operations:
$disk = Storage::disk('s3');$contents = Storage::disk('local')->get('file.jpg');
Retrieve files
The get method can be used to retrieve the content of a given object. This method returns the original string content of the file:
$contents = Storage::get('file.jpg');
The exists method can be used to determine whether the specified file exists on the disk:
$exists = Storage::disk('s3')->exists('file.jpg');
File URLs
When using the local or s3 driver, you can use the url method to obtain the URL of the specified file. If you are using a local driver, it simply adds the/storage prefix before the given path to return the relative file path. If you are using the s3 driver, the complete remote RUL will be returned:
$url = Storage::url('file1.jpg');
Note: When the local driver is used, make sure that the symbolic link from public/storage to storage/app/public is created.
File metadata
The size method can be used to obtain the byte size of a given object:
$size = Storage::size('file1.jpg');
The lastModified method returns the last modification time of the specified object. it uses the UNIX timestamp:
$time = Storage::lastModified('file1.jpg');
Storage files
The put method can be used to store files to disks. You can pass a PHP resource to the put method, then it will use the bottom layer of the file system support. When interacting with large files, file streams are recommended:
Storage::put('file.jpg', $contents);Storage::put('file.jpg', $resource);
The copy method can be used to copy existing files on a disk to a new location:
Storage::copy('old/file1.jpg', 'new/file1.jpg');
The move method can be used to modify the name of an existing file on the disk or move it to a new location:
Storage:move('old,file1.jpg', 'new/file1.jpg');
Pre-or append content to a file
The prepend and append methods allow you to easily add content to the start or end position of a file:
Storage::prepend('file.log', 'Prepended Text');Storage::append('file.log', 'Appended Text');
File visibility
You can use the getVisibility and setVisibility methods to retrieve and set file visibility. Visibility is the abstraction of cross-platform file permissions:
Storage::getVisibility('file.jpg');Storage::setVisibility('file.jpg', 'public');
In addition, you can set the object visibility while using the put method. Valid visibility values are public and private:
Storage::put('file.jpg', $contents, 'public');
Delete an object
The delete method can accept an array composed of file names or file names. it will delete the corresponding files from the disk:
Storage::delete('file.jpg');Storage::delete(['file1.jpg', 'file2.jpg']);
Directory
Retrieve all files from the Directory
The files method returns an array consisting of all files in the given directory. If you want to include the subdirectories of the given directory in the retrieved file. Then you need to use the allFiles method:
$file = Storage::files($directory);$files = Storage::allFiles($directory);
Retrieve all directories from a given Directory
The directories method returns an array composed of all subdirectories in the given directory. In addition, you can use the allDirectories method to recursively retrieve sub-directories:
$directories = Storage::directories($directory);// Recursive...$directories = Storage::allDirectories($directory);
Create directory
The makeDirectory method creates a given Directory, including the required subdirectories:
Storage::makeDirectory($directory);
Delete a directory
Finally, the deleteDirectory method can be used to delete a directory and delete all files in the directory on the disk:
Storage::deleteDirectory($directory);
Custom file system
Laravel's file system provides out-of-the-box support for several common storage systems. In fact, the file system does not limit you to only use the provided. You can create an adapter to build a custom driver to support your desired file storage system.
You need to create a service provider to build a custom file storage system. For example, DropboxServiceProvider. In the provider's boot method, you need to use the extend method of the Storage surface to define your own driver:
The first parameter in the extend method should be the driver name, and the second parameter is a closure. the closure accepts the $ app and $ config variables. The parsed closure must return an instance of League \ Flysystem \ Filesystem. The $ config variable contains the disk value specified in the config/filesystems. php file.
Once you have created a service provider and registered this extension, you can use the dropbox driver in the config/filesystem. php configuration file.