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',
'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');
// 创建一个文件
$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); //
}

最后修改时间:

    public function index()
{
// 取到磁盘实例
$disk = Storage::disk('local'); // 取出文件
$time = Storage::lastModified('file1.txt'); //
$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');
}

Laravel5.1 文件管理的更多相关文章

  1. Laravel5.1 搭建博客 --上传文件及文件管理

    教程源自:Laravel学院 这一节 咱来说说上传文件的功能实现,我们会把上传的文件保存到项目本地,不仅上传 还有删除和预览功能. 1 配置 我们先从配置开始做起,先修改我们自己创建的 blog.ph ...

  2. Linux安装LAMP开发环境及配置文件管理

    Linux主要分为两大系发行版,分别是RedHat和Debian,lamp环境的安装和配置也会有所不同,所以分别以CentOS 7.1和Ubuntu 14.04做为主机(L) Linux下安装软件,最 ...

  3. java springMVC SSM 操作日志 4级别联动 文件管理 头像编辑 shiro redis

    A 调用摄像头拍照,自定义裁剪编辑头像 B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,开发利器)+快速构建表单;  技术:313596790freemaker模版技术 ,0个代码不用写 ...

  4. BPM体系文件管理解决方案分享

    一.方案概述 企业管理在很大程度上是通过文件化的形式表现出来,体系文件管理是管理体系存在的基础和证据,是规范企业管理活动和全体人员行为,达到管理目标的管理依据.对与公司质量.环境.职业健康安全等体系有 ...

  5. 简单的学习心得:网易云课堂Android开发第五章SharedPreferences与文件管理

    一.SharedPreferences (1)SharedPreferences能够用来保存一些属于基本数据类型的数据. (2)保存数据,删除数据都是由SharedPreferences的内部接口Ed ...

  6. 归档NSKeyedArchiver解归档NSKeyedUnarchiver与文件管理类NSFileManager (文件操作)

    ========================== 文件操作 ========================== 一.归档NSKeyedArchiver 1.第一种方式:存储一种数据. // 归档 ...

  7. BZOJ 3289: Mato的文件管理[莫队算法 树状数组]

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2399  Solved: 988[Submit][Status][Di ...

  8. linux 基础命令与文件管理

      Linux终端介绍 Shell提示符 Bash Shell基本语法 基本命令的使用:ls.pwd.cd 查看系统和BIOS硬件时间 Linux如何获得帮助 Linux关机命令:shutdow.in ...

  9. laravel5 安装笔记

    1.环境更新 apt-get update apt-get install php5-cli apt-get install curl 2. Composer安装 curl -sS https://g ...

随机推荐

  1. Android 混淆打包不混淆第三方jar包

    项目由于要公布,所以要混淆打包. 混淆打包流程: 1.在proguard-project.txt文件里加入不须要混淆的类和第三方的jar包   这个是保持自己包中不须要混淆的类,假设有些类调用了jni ...

  2. WCF深入浅出学习1

    1.本文主要对WCF的基本使用做简单化介绍,对于初学WCF的来说,初期对于配置文件的理解,比较烦躁吧,相信你看完了该文,能够达到深入浅出的感觉. 关于WCF的概念 和 应用场景,在此处不做详细介绍,可 ...

  3. C# 基础: new 和 overrider 的区别

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. oracle 错误码查看命令oerr ora及常用错误码总结--不断更新

    oracle 错误码查看命令oerr ora及常用错误码总结--不断更新 1.ORA-00907: 缺失右括号 我自己的问题出在 字段的default 和 not null 顺序反了,defalut ...

  5. struts2 的国际化

    一.使用步骤 1)写资源文件,资源文件名命名规范和之前的讲的一致,有疑问请参考java开发中国际化 2)配置,在 struts.xml 中使用常量进行加载 struts.custom.i18n.res ...

  6. ZOJ 2610 Puzzle 模拟

    大模拟:枚举6个方向.检查每一个0是否能移动 Puzzle Time Limit: 2 Seconds      Memory Limit: 65536 KB Little Georgie likes ...

  7. 卡特兰数 catalan number

    作者:阿凡卢 出处:http://www.cnblogs.com/luxiaoxun/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留 ...

  8. Photoshop学习笔记(1)--界面设置

    根据慕课网教学视频整理(抄了一个妹子的评论): http://www.imooc.com/video/9813 ps版本:CS6 步骤: 1.视图->显示->智能参考线,以及视图-> ...

  9. 微博excel数据清洗(Java版)

    微博数据清洗(Java版) 原创 2013年12月10日 10:58:24 2979 大数据公益大学提供的一份数据,义务处理一下,原始数据是Excel,含有html标签,如下:   要求清洗掉html ...

  10. C++成员函数的存储方式

    用类去定义对象时,系统会为每一个对象分配存储空间.如果一个类包括了数据和函数,要分别为数据和函数的代码分配存储空间. 按理说,如果用同一个类定义了10个对象,那么就需要分别为10个对象的数据和函数代码 ...