SoFunction
Updated on 2025-04-04

Analysis of Laravel5.1 framework file management operation examples

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' =&gt; [
      'driver' =&gt; 'local',
// 'root' => storage_path('app'), in /storage/app/ directory      'root'  =&gt; 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-&gt;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-&gt;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-&gt;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-&gt;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-&gt;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-&gt;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-&gt;prepend('', 'test prepend');
    // Add at the end of the file    $disk-&gt;append('', 'test append');
  }

3.8 Delete files

  public function index()
  {
    // Get the disk instance    $disk = Storage::disk('local');
    // Delete a single file    $disk-&gt;delete('');
    // Delete multiple files    $disk-&gt;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-&gt;files($directory);
    // Get all files in the directory (including files in the subdirectory)    $allFiles = $disk-&gt;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-&gt;directories($directory);
    // Get all subdirectories in the directory (including subdirectories under the subdirectories)    $allDirectories = $disk-&gt;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-&gt;makeDirectory('test');
    $disk-&gt;makeDirectory('test1/test2');
  }

4.4 Delete Directory

  public function index()
  {
    // Get the disk instance    $disk = Storage::disk('local');
    // Delete the directory    $disk-&gt;deleteDirectory('test');
    $disk-&gt;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.