This article describes the Laravel5.1 framework file management operations. Share it for your reference, as follows:
Laravel provides a very useful file system that is convenient for managing folders and files, and supports drivers such as Amazon S3 and Rackspace cloud storage.
1 Configuration
The file system configuration file is in config/ and its annotations are written very clearly. In addition, you can create a new disk in the disks array:
<?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' => '', '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' => '/v2.0/', 'region' => 'IAD', 'url_type' => 'publicURL', ], ], ];
Generally speaking, local (local) storage is the most commonly used, so in particular, we can modify our root path by modifying 'root':
'local' => [ 'driver' => 'local', // 'root' => storage_path('app'), in /storage/app/ directory 'root' => public_path('uploads'), // in the public/uploads/ directory ],
2 Get the hard disk instance
To perform file management, we can obtain it through the disk method of the Storage facade, and then we can perform the operations we want:
public function index() { $disk = Storage::disk('local'); // Create a file $disk->put('', 'Laravel Storage'); }
3 File operations
3.1 Get the file
public function index() { // Get the disk instance $disk = Storage::disk('local'); // Remove the file $file = $disk->get(''); dd($file); }
We can use the get() method to get the file and pass the file name in the form of a string, but we need an idea: if you want to get the file below the subdirectory, you need to pass the path, such as: $disk->get('subpath/.../.../.../.../');
3.2 Determine whether the file exists
public function index() { // Get the disk instance $disk = Storage::disk('local'); // Remove the file $exists = $disk->exists(''); dd($exists); // false }
3.3 Obtain file information
File size:
public function index() { // Get the disk instance $disk = Storage::disk('local'); // Remove the file $size = Storage::size('/home/'); dd($size); // 4 }
Last modified time:
public function index() { // Get the disk instance $disk = Storage::disk('local'); // Remove the file $time = Storage::lastModified(''); // 1507701271 $time = Carbon::createFromTimestamp($time); echo $time; // 2017-10-11 05:54:31 }
3.4 Save files
public function index() { // Get the disk instance $disk = Storage::disk('local'); //Save files $disk->put('', 'file2 content'); // Create a new file }
3.5 Copy Files
public function index() { // Get the disk instance $disk = Storage::disk('local'); // Copy the file The first parameter is the file to be copied, and the second parameter is where to copy it $disk->copy('home/',''); }
3.6 Move files
public function index() { // Get the disk instance $disk = Storage::disk('local'); // Copy the file The first parameter is the file to be moved, and the second parameter is where to move $disk->move('', 'home/'); }
3.7 Add content at the beginning/end of the file
public function index() { // Get the disk instance $disk = Storage::disk('local'); // Add at the beginning of the file $disk->prepend('', 'test prepend'); // Add at the end of the file $disk->append('', 'test append'); }
3.8 Delete files
public function index() { // Get the disk instance $disk = Storage::disk('local'); // Delete a single file $disk->delete(''); // Delete multiple files $disk->delete(['', '']); }
4 Directory operations
4.1 Get the file in the directory
public function index() { // Get the disk instance $disk = Storage::disk('local'); $directory = '/'; // Get the file in the directory $files = $disk->files($directory); // Get all files in the directory (including files in the subdirectory) $allFiles = $disk->allFiles($directory); dd($files, $allFiles); }
4.2 Get subdirectory
public function index() { // Get the disk instance $disk = Storage::disk('local'); $directory = '/'; // Get the subdirectory in the directory $directories = $disk->directories($directory); // Get all subdirectories in the directory (including subdirectories under the subdirectories) $allDirectories = $disk->allDirectories($directory); dd($directories, $allDirectories); }
4.3 Create a directory
public function index() { // Get the disk instance $disk = Storage::disk('local'); // Create a directory $disk->makeDirectory('test'); $disk->makeDirectory('test1/test2'); }
4.4 Delete Directory
public function index() { // Get the disk instance $disk = Storage::disk('local'); // Delete the directory $disk->deleteDirectory('test'); $disk->deleteDirectory('test1/test2'); }
For more information about Laravel, readers who are interested in view the topic of this site:Laravel framework introduction and advanced tutorial》、《Summary of excellent development framework for php》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope that this article will be helpful to everyone's PHP programming based on the Laravel framework.