1. 程式人生 > 程式設計 >Laravel5.1 框架檔案管理操作例項分析

Laravel5.1 框架檔案管理操作例項分析

本文例項講述了Laravel5.1 框架檔案管理操作。分享給大家供大家參考,具體如下:

Laravel提供了一套很好用的檔案系統 方便於管理資料夾和檔案,支援Amazon S3和Rackspace雲端儲存等驅動。

1 配置

檔案系統的配置檔案在 config/filesyetems.php 中,且它的註釋寫的很清楚了,此外你可以在disks陣列中建立新的disk:

<?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'   => 'ftp.example.com','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','key'    => 'your-key','container' => 'your-container','endpoint' => 'https://identity.api.rackspacecloud.com/v2.0/','region'  => 'IAD','url_type' => 'publicURL',];

一般情況下最常用的是local(本地)儲存,所以特別說下,我們可以通過修改'root'來修改我們的root路徑:

    'local' => [
      'driver' => 'local',//      'root'  => storage_path('app'),在/storage/app/目錄
      'root'  => public_path('uploads'),// 在public/uploads/ 目錄
    ],

2 獲取硬碟例項

要進行檔案管理需要那到硬碟例項,我們可以通過 Storage 門面的 disk 方法來獲取,之後就可以進行我們想要的操作了:

  public function index()
  {
    $disk = Storage::disk('local');
    // 建立一個檔案
    $disk->put('file1.txt','Laravel Storage');
  }

3 檔案操作

3.1 獲取檔案

public function index()
  {
    // 取到磁碟例項
    $disk = Storage::disk('local');
    // 取出檔案
    $file = $disk->get('test.txt');
    dd($file);
  }

我們可以使用get()方法獲取到檔案 以字串的形式傳入檔名就行,但是需要主意:如果你要取到子目錄以下的檔案時需要傳入路徑,比如:$disk->get('subpath/.../.../.../file.txt');

3.2 判斷檔案是否存在

  public function index()
  {
    // 取到磁碟例項
    $disk = Storage::disk('local');
    // 取出檔案
    $exists = $disk->exists('image.png');
    dd($exists);  // false
  }

3.3 獲取檔案資訊

檔案大小:

  public function index()
  {
    // 取到磁碟例項
    $disk = Storage::disk('local');
    // 取出檔案
    $size = Storage::size('/home/test.txt');
    dd($size); // 4
  }

最後修改時間:

  public function index()
  {
    // 取到磁碟例項
    $disk = Storage::disk('local');
    // 取出檔案
    $time = Storage::lastModified('file1.txt');   // 1507701271
    $time = Carbon::createFromTimestamp($time);
    echo $time;   // 2017-10-11 05:54:31
  }

3.4 儲存檔案

  public function index()
  {
    // 取到磁碟例項
    $disk = Storage::disk('local');
    // 儲存檔案
    $disk->put('file2.txt','file2 content'); // 新建一個檔案
  }

3.5 Copy檔案

  public function index()
  {
    // 取到磁碟例項
    $disk = Storage::disk('local');
    // 拷貝檔案 第一個引數是要拷貝的檔案,第二個引數是拷貝到哪裡
    $disk->copy('home/test.txt','rename.txt');
  }

3.6 移動檔案

  public function index()
  {
    // 取到磁碟例項
    $disk = Storage::disk('local');
    // 拷貝檔案 第一個引數是要移動的檔案,第二個引數是移動到哪裡
    $disk->move('file2.txt','home/file.txt');
  }

3.7 在檔案開頭/結尾新增內容

  public function index()
  {
    // 取到磁碟例項
    $disk = Storage::disk('local');
    // 在檔案開頭新增
    $disk->prepend('file1.txt','test prepend');
    // 在檔案末尾新增
    $disk->append('file1.txt','test append');
  }

3.8 刪除檔案

  public function index()
  {
    // 取到磁碟例項
    $disk = Storage::disk('local');
    // 刪除單條檔案
    $disk->delete('test.txt');
    // 刪除多條檔案
    $disk->delete(['test22.txt','icon.jpg']);
  }

4 目錄操作

4.1 取到目錄下的檔案

  public function index()
  {
    // 取到磁碟例項
    $disk = Storage::disk('local');
    $directory = '/';
    // 獲取目錄下的檔案
    $files = $disk->files($directory);
    // 獲取目錄下的所有檔案(包括子目錄下的檔案)
    $allFiles = $disk->allFiles($directory);
    dd($files,$allFiles);
  }

4.2 取到子目錄

  public function index()
  {
    // 取到磁碟例項
    $disk = Storage::disk('local');
    $directory = '/';
    // 獲取目錄下的子目錄
    $directories = $disk->directories($directory);
    // 獲取目錄下的所有子目錄(包括子目錄下的子目錄)
    $allDirectories = $disk->allDirectories($directory);
    dd($directories,$allDirectories);
  }

4.3 建立目錄

  public function index()
  {
    // 取到磁碟例項
    $disk = Storage::disk('local');
    // 建立目錄
    $disk->makeDirectory('test');
    $disk->makeDirectory('test1/test2');
  }

4.4 刪除目錄

  public function index()
  {
    // 取到磁碟例項
    $disk = Storage::disk('local');
    // 刪除目錄
    $disk->deleteDirectory('test');
    $disk->deleteDirectory('test1/test2');
  }

更多關於Laravel相關內容感興趣的讀者可檢視本站專題:《Laravel框架入門與進階教程》、《php優秀開發框架總結》、《php面向物件程式設計入門教程》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧彙總》

希望本文所述對大家基於Laravel框架的PHP程式設計有所幫助。