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',
'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 文件管理的更多相关文章
- Laravel5.1 搭建博客 --上传文件及文件管理
教程源自:Laravel学院 这一节 咱来说说上传文件的功能实现,我们会把上传的文件保存到项目本地,不仅上传 还有删除和预览功能. 1 配置 我们先从配置开始做起,先修改我们自己创建的 blog.ph ...
- Linux安装LAMP开发环境及配置文件管理
Linux主要分为两大系发行版,分别是RedHat和Debian,lamp环境的安装和配置也会有所不同,所以分别以CentOS 7.1和Ubuntu 14.04做为主机(L) Linux下安装软件,最 ...
- java springMVC SSM 操作日志 4级别联动 文件管理 头像编辑 shiro redis
A 调用摄像头拍照,自定义裁剪编辑头像 B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,开发利器)+快速构建表单; 技术:313596790freemaker模版技术 ,0个代码不用写 ...
- BPM体系文件管理解决方案分享
一.方案概述 企业管理在很大程度上是通过文件化的形式表现出来,体系文件管理是管理体系存在的基础和证据,是规范企业管理活动和全体人员行为,达到管理目标的管理依据.对与公司质量.环境.职业健康安全等体系有 ...
- 简单的学习心得:网易云课堂Android开发第五章SharedPreferences与文件管理
一.SharedPreferences (1)SharedPreferences能够用来保存一些属于基本数据类型的数据. (2)保存数据,删除数据都是由SharedPreferences的内部接口Ed ...
- 归档NSKeyedArchiver解归档NSKeyedUnarchiver与文件管理类NSFileManager (文件操作)
========================== 文件操作 ========================== 一.归档NSKeyedArchiver 1.第一种方式:存储一种数据. // 归档 ...
- BZOJ 3289: Mato的文件管理[莫队算法 树状数组]
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 2399 Solved: 988[Submit][Status][Di ...
- linux 基础命令与文件管理
Linux终端介绍 Shell提示符 Bash Shell基本语法 基本命令的使用:ls.pwd.cd 查看系统和BIOS硬件时间 Linux如何获得帮助 Linux关机命令:shutdow.in ...
- laravel5 安装笔记
1.环境更新 apt-get update apt-get install php5-cli apt-get install curl 2. Composer安装 curl -sS https://g ...
随机推荐
- Apache Directory Studio
Apache Directory Studio 是一个 LDAP 的工具平台,用来连接到任何 LDAP 服务器并进行管理和开发工作.主要功能:LDAP浏览器.LDIF编辑器.嵌入式 ApacheDS. ...
- Mysql 修改数据库,mysql修改表类型,Mysql增加表字段,Mysql删除表字段,Mysql修改字段名,Mysql修改字段排列顺序,Mysql修改表名
对于已经创建好的表,尤其是已经有大量数据的表,如果需要对表做一些结构上的改变,我们可以先将表删除(drop),然后再按照新的表定义重建表.这样做没有问题,但是必然要做一些额外的工作,比如数据的重新加载 ...
- 连接到 Linux 服务器时首先要运行的 5 个命令
作为一个系统管理员/SRE 工作 5 年后,我知道当我连接到一台 Linux 服务器时我首先应该做什么.这里有一系列关于服务器你必须了解的信息,以便你可以(在大部分时间里)更好的调试该服务器. 连上 ...
- C++ 简单的日志类
用法如下: #include "EasyLog.h" int main(){ EasyLog::Inst()->Log("Run..."); } 不只是m ...
- 每日英语:Stalled Project Shows Why China's Economy Is Wobbling
CAOFEIDIAN, China $91 billion industrial project here, mired in debt and unfulfilled promise, sugge ...
- iOS开发-iOS 10 由于权限问题导致崩溃的那些坑
iOS开发-iOS 10 由于权限问题导致崩溃的那些坑 6月份的WWDC大会结束有一段时间了,相信很多开发者也是在努力工作的闲时用着Xcode8 Beta版学习着新的特性吧. 使用Xcode8写自己 ...
- 彻底征服 Spring AOP 之 理论篇
基本知识 其实, 接触了这么久的 AOP, 我感觉, AOP 给人难以理解的一个关键点是它的概念比较多, 而且坑爹的是, 这些概念经过了中文翻译后, 变得面目全非, 相同的一个术语, 在不同的翻译下, ...
- sql 记录
INSERT INTO B([name],[info]) SELECT [name,'10'] FROM A 级联更新1:update tb1, tb2 set tb1.a=tb2.a,tb1.b=t ...
- 基于jQ+CSS3页面滚动内容元素动画特效
今天给大家分享一款基于jQ+CSS3页面滚动内容元素动画特效.这是一款基于jQuery+CSS3实现的页面滚动代码.该实例适用于适用浏览器:360.FireFox.Chrome.Safari.Oper ...
- Unix系统编程()深入探究文件IO概述
open调用将引入原子atomicity操作的概念. 将某一系统调用所要完成的各个动作作为不可中断的操作,一次性加以执行. 原子操作是许多系统调用得以正确执行的必要条件. 还介绍一个系统调用fcntl ...