Laravel5.1 框架文件管理操作實例分析
本文實例講述了Laravel5.1 框架文件管理操作。分享給大家供大家參考,具體如下:
Laravel提供了一套很好用的文件系統(tǒng) 方便于管理文件夾和文件,支持Amazon S3和Rackspace云存儲等驅(qū)動。
1 配置
文件系統(tǒng)的配置文件在 config/filesyetems.php 中,且它的注釋寫的很清楚了,此外你可以在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(本地)存儲,所以特別說下,我們可以通過修改'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 獲取文件信息
文件大?。?/p>
  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');
    // 拷貝文件 第一個參數(shù)是要拷貝的文件,第二個參數(shù)是拷貝到哪里
    $disk->copy('home/test.txt','rename.txt');
  }
3.6 移動文件
  public function index()
  {
    // 取到磁盤實例
    $disk = Storage::disk('local');
    // 拷貝文件 第一個參數(shù)是要移動的文件,第二個參數(shù)是移動到哪里
    $disk->move('file2.txt', 'home/file.txt');
  }
3.7 在文件開頭/結(jié)尾添加內(nèi)容
  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');
  }
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Laravel框架的PHP程序設(shè)計有所幫助。
上一篇:Laravel5.1 框架關(guān)聯(lián)模型之后操作實例分析
欄 目:PHP編程
下一篇:詳解Laravel5.6通過路由進行API版本控制的簡單方法
本文標(biāo)題:Laravel5.1 框架文件管理操作實例分析
本文地址:http://www.jygsgssxh.com/a1/PHPbiancheng/10968.html
您可能感興趣的文章
- 04-02php如何用導(dǎo)入數(shù)據(jù) php用來導(dǎo)入其他文件的語句
 - 01-11thinkphp框架類庫擴展操作示例
 - 01-11關(guān)于Yii2框架跑腳本時內(nèi)存泄漏問題的分析與解決
 - 01-11php 實現(xiàn)簡單的登錄功能示例【基于thinkPHP框架】
 - 01-11php 使用expat方式解析xml文件操作示例
 - 01-11Laravel框架Blade模板簡介及模板繼承用法分析
 - 01-11Laravel框架基礎(chǔ)語法與知識點整理【模板變量、輸出、include引入
 - 01-11Laravel框架Eloquent ORM刪除數(shù)據(jù)操作示例
 - 01-11Laravel框架Eloquent ORM修改數(shù)據(jù)操作示例
 - 01-11Laravel框架Eloquent ORM簡介、模型建立及查詢數(shù)據(jù)操作詳解
 


閱讀排行
本欄相關(guān)
- 04-02php本站才可以請求數(shù)據(jù) php本地數(shù)據(jù)庫
 - 04-02關(guān)于txt數(shù)據(jù)庫php的信息
 - 04-02php打印請求數(shù)據(jù) php打印輸出結(jié)果
 - 04-02網(wǎng)頁里php操作數(shù)據(jù)庫 php網(wǎng)頁例子
 - 04-02php插入數(shù)據(jù)庫為亂碼 php連接數(shù)據(jù)庫亂
 - 04-02php數(shù)據(jù)庫地址 phpstudy 數(shù)據(jù)庫
 - 04-02php數(shù)據(jù)庫數(shù)據(jù)相加 php數(shù)據(jù)庫添加數(shù)據(jù)
 - 04-02數(shù)據(jù)權(quán)限架構(gòu)思路php 數(shù)據(jù)權(quán)限設(shè)計方
 - 04-02php數(shù)據(jù)庫輸入變量 php里輸出數(shù)據(jù)庫數(shù)
 - 04-02php如何用導(dǎo)入數(shù)據(jù) php用來導(dǎo)入其他文
 
隨機閱讀
- 01-10C#中split用法實例總結(jié)
 - 01-11ajax實現(xiàn)頁面的局部加載
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 - 01-10delphi制作wav文件的方法
 - 04-02jquery與jsp,用jquery
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 08-05織夢dedecms什么時候用欄目交叉功能?
 


