亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Laravel5.1 框架文件管理操作實(shí)例分析

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

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

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

1 配置

文件系統(tǒng)的配置文件在 config/filesyetems.php 中,且它的注釋寫(xiě)的很清楚了,此外你可以在disks數(shù)組中創(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(本地)存儲(chǔ),所以特別說(shuō)下,我們可以通過(guò)修改'root'來(lái)修改我們的root路徑:

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

2 獲取硬盤實(shí)例

要進(jìn)行文件管理需要那到硬盤實(shí)例,我們可以通過(guò) Storage 門面的 disk 方法來(lái)獲取,之后就可以進(jìn)行我們想要的操作了:

  public function index()
  {
    $disk = Storage::disk('local');
    // 創(chuàng)建一個(gè)文件
    $disk->put('file1.txt', 'Laravel Storage');
  }

3 文件操作

3.1 獲取文件

public function index()
  {
    // 取到磁盤實(shí)例
    $disk = Storage::disk('local');
    // 取出文件
    $file = $disk->get('test.txt');
    dd($file);
  }

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

3.2 判斷文件是否存在

  public function index()
  {
    // 取到磁盤實(shí)例
    $disk = Storage::disk('local');
    // 取出文件
    $exists = $disk->exists('image.png');
    dd($exists);  // false
  }

3.3 獲取文件信息

文件大?。?/p>

  public function index()
  {
    // 取到磁盤實(shí)例
    $disk = Storage::disk('local');
    // 取出文件
    $size = Storage::size('/home/test.txt');
    dd($size); // 4
  }

最后修改時(shí)間:

  public function index()
  {
    // 取到磁盤實(shí)例
    $disk = Storage::disk('local');
    // 取出文件
    $time = Storage::lastModified('file1.txt');   // 1507701271
    $time = Carbon::createFromTimestamp($time);
    echo $time;   // 2017-10-11 05:54:31
  }

3.4 儲(chǔ)存文件

  public function index()
  {
    // 取到磁盤實(shí)例
    $disk = Storage::disk('local');
    // 儲(chǔ)存文件
    $disk->put('file2.txt', 'file2 content'); // 新建一個(gè)文件
  }

3.5 Copy文件

  public function index()
  {
    // 取到磁盤實(shí)例
    $disk = Storage::disk('local');
    // 拷貝文件 第一個(gè)參數(shù)是要拷貝的文件,第二個(gè)參數(shù)是拷貝到哪里
    $disk->copy('home/test.txt','rename.txt');
  }

3.6 移動(dòng)文件

  public function index()
  {
    // 取到磁盤實(shí)例
    $disk = Storage::disk('local');
    // 拷貝文件 第一個(gè)參數(shù)是要移動(dòng)的文件,第二個(gè)參數(shù)是移動(dòng)到哪里
    $disk->move('file2.txt', 'home/file.txt');
  }

3.7 在文件開(kāi)頭/結(jié)尾添加內(nèi)容

  public function index()
  {
    // 取到磁盤實(shí)例
    $disk = Storage::disk('local');
    // 在文件開(kāi)頭添加
    $disk->prepend('file1.txt', 'test prepend');
    // 在文件末尾添加
    $disk->append('file1.txt', 'test append');
  }

3.8 刪除文件

  public function index()
  {
    // 取到磁盤實(shí)例
    $disk = Storage::disk('local');
    // 刪除單條文件
    $disk->delete('test.txt');
    // 刪除多條文件
    $disk->delete(['test22.txt', 'icon.jpg']);
  }

4 目錄操作

4.1 取到目錄下的文件

  public function index()
  {
    // 取到磁盤實(shí)例
    $disk = Storage::disk('local');
    $directory = '/';
    // 獲取目錄下的文件
    $files = $disk->files($directory);
    // 獲取目錄下的所有文件(包括子目錄下的文件)
    $allFiles = $disk->allFiles($directory);
    dd($files, $allFiles);
  }

4.2 取到子目錄

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

4.3 創(chuàng)建目錄

  public function index()
  {
    // 取到磁盤實(shí)例
    $disk = Storage::disk('local');
    // 創(chuàng)建目錄
    $disk->makeDirectory('test');
    $disk->makeDirectory('test1/test2');
  }

4.4 刪除目錄

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

更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • php遞歸使用示例(php遞歸函數(shù))

    php遞歸使用示例(php遞歸函數(shù))

    這篇文章主要介紹了php遞歸使用示例(php遞歸函數(shù)),包括遞歸獲得角色I(xiàn)D字符串、遞歸獲取級(jí)聯(lián)角色信息數(shù)組、通過(guò)父角色的id獲取子角色信息,需要的朋友可以參考下
    2014-02-02
  • 新浪微博API開(kāi)發(fā)簡(jiǎn)介之用戶授權(quán)(PHP基礎(chǔ)篇)

    新浪微博API開(kāi)發(fā)簡(jiǎn)介之用戶授權(quán)(PHP基礎(chǔ)篇)

    己在開(kāi)發(fā)和學(xué)習(xí)的過(guò)程中,感覺(jué)雖然沒(méi)有太大難度,但還是有一些問(wèn)題是需要我們注意的,今天就我在開(kāi)發(fā)和學(xué)習(xí)的過(guò)程中,簡(jiǎn)單的對(duì)利用PHP進(jìn)行新浪微博API開(kāi)發(fā)的內(nèi)容進(jìn)行一個(gè)整理和說(shuō)明
    2011-09-09
  • 基于PHP中的常用函數(shù)回顧

    基于PHP中的常用函數(shù)回顧

    以下是對(duì)PHP中的常用函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下
    2013-07-07
  • laravel實(shí)現(xiàn)按月或天或小時(shí)統(tǒng)計(jì)mysql數(shù)據(jù)的方法

    laravel實(shí)現(xiàn)按月或天或小時(shí)統(tǒng)計(jì)mysql數(shù)據(jù)的方法

    今天小編就為大家分享一篇laravel實(shí)現(xiàn)按月或天或小時(shí)統(tǒng)計(jì)mysql數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-10-10
  • 探討:array2xml和xml2array以及xml與array的互相轉(zhuǎn)化

    探討:array2xml和xml2array以及xml與array的互相轉(zhuǎn)化

    本篇文章是對(duì)array2xml和xml2array以及xml與array的互相轉(zhuǎn)化進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • php多文件上傳下載示例分享

    php多文件上傳下載示例分享

    這篇文章主要介紹了php多文件上傳下載示例,需要的朋友可以參考下
    2014-02-02
  • PHP微信API接口類

    PHP微信API接口類

    很全面詳細(xì)的PHP微信API接口類,幫助大家更好的進(jìn)行php微信開(kāi)發(fā),感興趣的小伙伴們可以參考一下
    2016-08-08
  • 開(kāi)啟PHP Static 關(guān)鍵字之旅模式

    開(kāi)啟PHP Static 關(guān)鍵字之旅模式

    靜態(tài)成員是一種類變量,可以把它看成時(shí)屬于整個(gè)類而不是屬于類的某個(gè)實(shí)例。與一般的實(shí)例變量不同的是,靜態(tài)成員只保留一個(gè)變量值,而這個(gè)變量值對(duì)所有的實(shí)例都是有效的,也就是說(shuō),所有的實(shí)例共享這個(gè)成員,跟著小編一起去探討php static關(guān)鍵字吧
    2015-11-11
  • php實(shí)現(xiàn)下載限制速度示例分享

    php實(shí)現(xiàn)下載限制速度示例分享

    這篇文章主要介紹了php實(shí)現(xiàn)限制下載速度的示例,需要的朋友可以參考下
    2014-02-02
  • PHP如何防止用戶重復(fù)提交表單

    PHP如何防止用戶重復(fù)提交表單

    這篇文章主要介紹了PHP如何防止用戶重復(fù)提交表單,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12

最新評(píng)論