laravel文件存储、删除、移动等操作
laravel文件存储、删除、移动等操作
一、总结
一句话总结:
启示:可以在操作遇到问题的时候,找文档找实例好好实验一下,也就是学习巩固一下,不必一定要死纠排错
1、laravel文件删除注意?
1、注意disk:disk决定路径
2、删单个文件的时候就用删单个文件的方式,别用删多个文件的方式(也就是参数别数组)
public function index()
{
// 取到磁盘实例
$disk = Storage::disk('local'); // 删除单条文件
$disk->delete('test.txt');
// 删除多条文件
$disk->delete(['test22.txt', 'icon.jpg']);
}
二、laravel文件存储、删除、移动等操作
转自或参考:laravel文件存储、删除、移动等操作
https://blog.csdn.net/huangjinao/article/details/85867101
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);  // 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 upload(Request $request){
    if ($request->isMethod('POST')){
        $file = $request->file('source');
        //判断文件是否上传成功
        if ($file->isValid()){
            //原文件名
            $originalName = $file->getClientOriginalName();
            //扩展名
            $ext = $file->getClientOriginalExtension();
            //MimeType
            $type = $file->getClientMimeType();
            //临时绝对路径
            $realPath = $file->getRealPath();
            $filename = uniqid().'.'.$ext;
            $bool = Storage::disk('uploads')->put($originalName,file_get_contents($realPath));
            //判断是否上传成功
            if($bool){
                echo 'success';
            }else{
                echo 'fail';
            }
        }
    }
    return view('upload');
}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']);
    }3.8 下载文件
 public function download(){
        $data = './uploads/20190104/5c2f6248614a7.avi';
        return response()->download($data);
}
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');
    }laravel文件存储、删除、移动等操作的更多相关文章
- WinForm中使用XML文件存储用户配置及操作本地Config配置文件
		大家都开发winform程序时候会大量用到配置App.config作为保持用户设置的基本信息,比如记住用户名,这样的弊端就是每个人一些个性化的设置每次更新程序的时候会被覆盖. 故将配置文件分两大类: ... 
- Win8 Metro中文件读写删除与复制操作
		Win8Metro中,我们不能在向以前那样调用WIN32的API函数来进行文件操作,因此,下面就来介绍一下Win8 Metro中文件的读写操作. 1 Windows 8 Metro Style App ... 
- WinForm中使用XML文件存储用户配置及操作本地Config配置文件(zt)
		因项目中采用CS结构读取Web.config文件,故参照一下的连接完成此功能,在此感谢原作者! 原文地址: http://www.cnblogs.com/zfanlong1314/p/3623622. ... 
- laravel文件存储Storage
		use Illuminate\Support\Facades\Storage; //建立目录 Storage::disk('public')->makeDirectory(date('Y-m') ... 
- Delphi阿里云对象存储OSS【支持上传文件、下载文件、删除文件、创建目录、删除目录、Bucket操作等】
		作者QQ:(648437169) 点击下载➨Delphi阿里云对象存储OSS 阿里云api文档 [Delphi阿里云对象存储OSS]支持 获取Bucket列表.设置Bucket ... 
- Laravel 的文件存储 - Storage
		记录一下 Laravel Storage 的常见用法 内容写入磁盘文件 > php artisan tinker >>> use Illuminate\Support\Faca ... 
- Java读取json文件并对json数据进行读取、添加、删除与修改操作
		转载:http://blog.csdn.net/qing_yun/article/details/46865863#t0 1.介绍 开发过程中经常会遇到json数据的处理,而单独对json数据进行 ... 
- ASP FSO操作文件(复制文件、重命名文件、删除文件、替换字符串)
		ASP FSO操作文件(复制文件.重命名文件.删除文件.替换字符串)FSO的意思是FileSystemObject,即文件系统对象.FSO对象模型包含在Scripting 类型库 (Scrrun.Dl ... 
- thinkphp对文件的上传,删除,下载操作
		工作需要,整理一下最近对php的学习经验,希望能对自己有帮助或者能帮助那些需要帮助的人. thinkphp对文件的操作,相对来说比较简单,因为tp封装好了一个上传类Upload.class.php 废 ... 
随机推荐
- linux识别ntfs U盘
			NTFS-3G 是一个开源的软件,可以实现 Linux.Free BSD.Mac OSX.NetBSD 和 Haiku 等操作系统中的 NTFS 读写支持 下载最新ntfs-3g源代码,编译安装 # ... 
- iOS配置TARGETS
			说一下背景 自从这个项目不死不活的迭代了2年,从项目搭建到现在,一直都是自己在开发和维护,所以项目结构非常清晰,但是之前的水平写的代码现在看来也是惨不忍睹,不过本人比较懒,也就没有考虑过重构的事情 - ... 
- 根据值获取枚举类对象工具类EnumUtils
			枚举类 public enum Sex { man("M","男"),woman("W","女"); private S ... 
- c# MemoryStream 类
- python两则99乘法表
			分别应用while和for的嵌套循环,适用于初学的人看看 x = 1 while x <= 9: y = 1 while y <= x: print (y,'*',x,'=',x*y,en ... 
- zabbix Server 4.0 触发器(Trigger)篇
			zabbix Server 4.0 触发器(Trigger)篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.触发器(Trigger)概述 1>.上一篇博客我们介绍了“内 ... 
- k8s安装之etcd备份还原yaml
			etcd备份还原方案,这种比较高级. 使用docker,自动化处理. 如果单节点备份,ETCD_ENDPOINTS一个即可. 如果多节点恢复,依次执行恢复脚本即可. apiVersion: batch ... 
- 【Low版】HAUT - OJ - Contest1035 - 2017届新生周赛(六)题解
			问题 A: 比赛 时间限制: 2 秒 内存限制: 256 MB | 提交: 393 解决: 98提交 状态 题目描述 学校要派6名同学组成两个队(一个队3个人)去参加比赛,每个同学有一个分数,学校希望 ... 
- 原生JavaScript和jQuery的较量
			JavaScript和jQuery有很多相似知促,那么二者又是如何进行较量,我们先了解一下什么是JavaScript和jQuery,知其源头,才能知其所以然. 简介: [JavaScript] 一种直 ... 
- keras模块学习之泛型模型学习笔记
			本笔记由博客园-圆柱模板 博主整理笔记发布,转载需注明,谢谢合作! Keras泛型模型接口是: 用户定义多输出模型.非循环有向模型或具有共享层的模型等复杂模型的途径 适用于实现:全连接网络和多输入 ... 
