最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Laravel5.1 框架文件管理操作實例分析

 更新時間:2020年01月09日 11:19:46   作者:Sky_sunkang  
這篇文章主要介紹了Laravel5.1 框架文件管理操作,結合實例形式分析了laravel5.1框架文件管理相關的配置、磁盤獲取以及文件目錄操作技巧,需要的朋友可以參考下

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

Laravel提供了一套很好用的文件系統(tǒng) 方便于管理文件夾和文件,支持Amazon S3和Rackspace云存儲等驅動。

1 配置

文件系統(tǒng)的配置文件在 config/filesyetems.php 中,且它的注釋寫的很清楚了,此外你可以在disks數組中創(chuàng)建新的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',
      'username' => 'your-username',
      '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');
    // 創(chuàng)建一個文件
    $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 創(chuàng)建目錄

  public function index()
  {
    // 取到磁盤實例
    $disk = Storage::disk('local');
    // 創(chuàng)建目錄
    $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優(yōu)秀開發(fā)框架總結》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總

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

相關文章

  • PHP內核學習教程之php opcode內核實現

    PHP內核學習教程之php opcode內核實現

    opcode是計算機指令中的一部分,用于指定要執(zhí)行的操作, 指令的格式和規(guī)范由處理器的指令規(guī)范指定,通過本文給大家介紹PHP內核學習教程之php opcode內核實現,感興趣的朋友一起學習吧
    2016-01-01
  • PHP設計模式之適配器模式(Adapter)原理與用法詳解

    PHP設計模式之適配器模式(Adapter)原理與用法詳解

    這篇文章主要介紹了PHP設計模式之適配器模式(Adapter)原理與用法,結合實例形式詳細分析了適配器模式的概念、原理、使用方法及相關操作注意事項,需要的朋友可以參考下
    2019-12-12
  • PHP設計模式之適配器模式代碼實例

    PHP設計模式之適配器模式代碼實例

    這篇文章主要介紹了PHP設計模式之適配器模式代碼實例,本文講解了目標、角色、應用場景、優(yōu)勢等內容,并給出代碼實例,需要的朋友可以參考下
    2015-05-05
  • php生成條形碼的圖片的實例詳解

    php生成條形碼的圖片的實例詳解

    這篇文章主要介紹了php生成條形碼的圖片的實例詳解的相關資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • PHP實現簡單網站訪客統(tǒng)計的方法實例

    PHP實現簡單網站訪客統(tǒng)計的方法實例

    這篇文章主要給大家介紹了關于PHP實現簡單網站訪客統(tǒng)計的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • php把session寫入數據庫示例

    php把session寫入數據庫示例

    這篇文章主要介紹了php把session寫入數據庫示例,需要的朋友可以參考下
    2014-02-02
  • php實現產品加入購物車功能(1)

    php實現產品加入購物車功能(1)

    這篇文章主要為大家詳細介紹了php實現產品加入購物車功能,具有一定的參考價值,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • thinkphp實現like模糊查詢實例

    thinkphp實現like模糊查詢實例

    這篇文章主要介紹了thinkphp實現like模糊查詢,以實例形式講述了字符串形式及數組形式作為查詢條件的like模糊查詢實現方法,是非常具有實用價值的技巧,需要的朋友可以參考下
    2014-10-10
  • php 分頁函數multi() discuz

    php 分頁函數multi() discuz

    discuz摘出來的php分頁函數multi(),大家以后也可以從discuz來獲取各種比較好的函數了,學習要注意借鑒。
    2009-06-06
  • 完善CodeIgniter在IDE中代碼提示功能的方法

    完善CodeIgniter在IDE中代碼提示功能的方法

    這篇文章主要介紹了完善CodeIgniter在IDE中代碼提示功能的方法,需要的朋友可以參考下
    2014-07-07

最新評論

泉州市| 红原县| 靖江市| 成安县| 石城县| 富宁县| 秀山| 西宁市| 观塘区| 阳曲县| 崇文区| 孝感市| 巴彦县| 博湖县| 天祝| 疏附县| 东丽区| 理塘县| 邢台县| 赤峰市| 阜阳市| 会泽县| 兴隆县| 枝江市| 柳州市| 龙门县| 沙坪坝区| 焦作市| 射阳县| 平度市| 武穴市| 包头市| 景宁| 阿合奇县| 正宁县| 女性| 贵州省| 遂川县| 宜川县| 建阳市| 玉树县|