laravel文件存储、删除、移动等操作

一、总结

一句话总结:

启示:可以在操作遇到问题的时候,找文档找实例好好实验一下,也就是学习巩固一下,不必一定要死纠排错

1、laravel文件删除注意?

1、注意disk:disk决定路径
2、删单个文件的时候就用删单个文件的方式,别用删多个文件的方式(也就是参数别数组)
  1. public function index()
  2. {
  3. // 取到磁盘实例
  4. $disk = Storage::disk('local');
  5.  
  6. // 删除单条文件
  7. $disk->delete('test.txt');
  8. // 删除多条文件
  9. $disk->delete(['test22.txt', 'icon.jpg']);
  10. }

二、laravel文件存储、删除、移动等操作

转自或参考:laravel文件存储、删除、移动等操作
https://blog.csdn.net/huangjinao/article/details/85867101

1 配置

文件系统的配置文件在 config/filesyetems.php 中,且它的注释写的很清楚了,此外你可以在disks数组中创建新的disk:

<?php

  1. return [
  2. /*
  3. |--------------------------------------------------------------------------
  4. | Default Filesystem Disk
  5. |--------------------------------------------------------------------------
  6. |
  7. | Here you may specify the default filesystem disk that should be used
  8. | by the framework. A "local" driver, as well as a variety of cloud
  9. | based drivers are available for your choosing. Just store away!
  10. |
  11. | Supported: "local", "ftp", "s3", "rackspace"
  12. |
  13. */
  14. 'default' => 'local',
  15. /*
  16. |--------------------------------------------------------------------------
  17. | Default Cloud Filesystem Disk
  18. |--------------------------------------------------------------------------
  19. |
  20. | Many applications store files both locally and in the cloud. For this
  21. | reason, you may specify a default "cloud" driver here. This driver
  22. | will be bound as the Cloud disk implementation in the container.
  23. |
  24. */
  25. 'cloud' => 's3',
  26. /*
  27. |--------------------------------------------------------------------------
  28. | Filesystem Disks
  29. |--------------------------------------------------------------------------
  30. |
  31. | Here you may configure as many filesystem "disks" as you wish, and you
  32. | may even configure multiple disks of the same driver. Defaults have
  33. | been setup for each driver as an example of the required options.
  34. |
  35. */
  36. 'disks' => [
  37. 'local' => [
  38. 'driver' => 'local',
  39. 'root' => storage_path('app'),
  40. ],
  41. 'ftp' => [
  42. 'driver' => 'ftp',
  43. 'host' => 'ftp.example.com',
  44. 'username' => 'your-username',
  45. 'password' => 'your-password',
  46. // Optional FTP Settings...
  47. // 'port' => 21,
  48. // 'root' => '',
  49. // 'passive' => true,
  50. // 'ssl' => true,
  51. // 'timeout' => 30,
  52. ],
  53. 's3' => [
  54. 'driver' => 's3',
  55. 'key' => 'your-key',
  56. 'secret' => 'your-secret',
  57. 'region' => 'your-region',
  58. 'bucket' => 'your-bucket',
  59. ],
  60. 'rackspace' => [
  61. 'driver' => 'rackspace',
  62. 'username' => 'your-username',
  63. 'key' => 'your-key',
  64. 'container' => 'your-container',
  65. 'endpoint' => 'https://identity.api.rackspacecloud.com/v2.0/',
  66. 'region' => 'IAD',
  67. 'url_type' => 'publicURL',
  68. ],
  69. ],
  70. ];

一般情况下最常用的是local(本地)存储,所以特别说下,我们可以通过修改'root'来修改我们的root路径:

  1. 'local' => [
  2. 'driver' => 'local',
  3. // 'root' => storage_path('app'), 在/storage/app/目录
  4. 'root' => public_path('uploads'), // 在public/uploads/ 目录
  5. ],

2 获取硬盘实例

要进行文件管理需要那到硬盘实例,我们可以通过 Storage 门面的 disk 方法来获取,之后就可以进行我们想要的操作了:

public function index()

  1. {
  2. $disk = Storage::disk('local');
  3. // 创建一个文件
  4. $disk->put('file1.txt', 'Laravel Storage');
  5. }

3 文件操作

3.1 获取文件

  1. public function index()
  2. {
  3. // 取到磁盘实例
  4. $disk = Storage::disk('local');
  5. // 取出文件
  6. $file = $disk->get('test.txt');
  7. dd($file);
  8. }

我们可以使用get()方法获取到文件 以字符串的形式传入文件名就行,但是需要主意:如果你要取到子目录以下的文件时需要传入路径,比如:$disk->get('subpath/.../.../.../file.txt');

3.2 判断文件是否存在

  1. public function index()
  2. {
  3. // 取到磁盘实例
  4. $disk = Storage::disk('local');
  5. // 取出文件
  6. $exists = $disk->exists('image.png');
  7. dd($exists); // false
  8. }

3.3 获取文件信息

文件大小:

public function index()

  1. {
  2. // 取到磁盘实例
  3. $disk = Storage::disk('local');
  4. // 取出文件
  5. $size = Storage::size('/home/test.txt');
  6. dd($size); // 4
  7. }

最后修改时间:

public function index()

  1. {
  2. // 取到磁盘实例
  3. $disk = Storage::disk('local');
  4. // 取出文件
  5. $time = Storage::lastModified('file1.txt'); // 1507701271
  6. $time = Carbon::createFromTimestamp($time);
  7. echo $time; // 2017-10-11 05:54:31
  8. }

3.4 储存文件

  1. public function upload(Request $request){
  2. if ($request->isMethod('POST')){
  3. $file = $request->file('source');
  4. //判断文件是否上传成功
  5. if ($file->isValid()){
  6. //原文件名
  7. $originalName = $file->getClientOriginalName();
  8. //扩展名
  9. $ext = $file->getClientOriginalExtension();
  10. //MimeType
  11. $type = $file->getClientMimeType();
  12. //临时绝对路径
  13. $realPath = $file->getRealPath();
  14. $filename = uniqid().'.'.$ext;
  15. $bool = Storage::disk('uploads')->put($originalName,file_get_contents($realPath));
  16. //判断是否上传成功
  17. if($bool){
  18. echo 'success';
  19. }else{
  20. echo 'fail';
  21. }
  22. }
  23. }
  24. return view('upload');
  25. }

3.5 Copy文件

  1. public function index()
  2. {
  3. // 取到磁盘实例
  4. $disk = Storage::disk('local');
  5. // 拷贝文件 第一个参数是要拷贝的文件,第二个参数是拷贝到哪里
  6. $disk->copy('home/test.txt','rename.txt');
  7. }

3.6 移动文件

  1. public function index()
  2. {
  3. // 取到磁盘实例
  4. $disk = Storage::disk('local');
  5. // 拷贝文件 第一个参数是要移动的文件,第二个参数是移动到哪里
  6. $disk->move('file2.txt', 'home/file.txt');
  7. }

3.7 在文件开头/结尾添加内容

  1. public function index()
  2. {
  3. // 取到磁盘实例
  4. $disk = Storage::disk('local');
  5. // 在文件开头添加
  6. $disk->prepend('file1.txt', 'test prepend');
  7. // 在文件末尾添加
  8. $disk->append('file1.txt', 'test append');
  9. }

3.8 删除文件

  1. public function index()
  2. {
  3. // 取到磁盘实例
  4. $disk = Storage::disk('local');
  5. // 删除单条文件
  6. $disk->delete('test.txt');
  7. // 删除多条文件
  8. $disk->delete(['test22.txt', 'icon.jpg']);
  9. }

3.8 下载文件

  1. public function download(){
  2. $data = './uploads/20190104/5c2f6248614a7.avi';
  3. return response()->download($data);

}

4目录操作

4.1 取到目录下的文件

  1. public function index()
  2. {
  3. // 取到磁盘实例
  4. $disk = Storage::disk('local');
  5. $directory = '/';
  6. // 获取目录下的文件
  7. $files = $disk->files($directory);
  8. // 获取目录下的所有文件(包括子目录下的文件)
  9. $allFiles = $disk->allFiles($directory);
  10. dd($files, $allFiles);
  11. }

4.2 取到子目录

  1. public function index()
  2. {
  3. // 取到磁盘实例
  4. $disk = Storage::disk('local');
  5. $directory = '/';
  6. // 获取目录下的子目录
  7. $directories = $disk->directories($directory);
  8. // 获取目录下的所有子目录(包括子目录下的子目录)
  9. $allDirectories = $disk->allDirectories($directory);
  10. dd($directories, $allDirectories);
  11. }

4.3 创建目录

  1. public function index()
  2. {
  3. // 取到磁盘实例
  4. $disk = Storage::disk('local');
  5. // 创建目录
  6. $disk->makeDirectory('test');
  7. $disk->makeDirectory('test1/test2');
  8. }

4.4 删除目录

  1. public function index()
  2. {
  3. // 取到磁盘实例
  4. $disk = Storage::disk('local');
  5. // 删除目录
  6. $disk->deleteDirectory('test');
  7. $disk->deleteDirectory('test1/test2');
  8. }
 

laravel文件存储、删除、移动等操作的更多相关文章

  1. WinForm中使用XML文件存储用户配置及操作本地Config配置文件

    大家都开发winform程序时候会大量用到配置App.config作为保持用户设置的基本信息,比如记住用户名,这样的弊端就是每个人一些个性化的设置每次更新程序的时候会被覆盖. 故将配置文件分两大类: ...

  2. Win8 Metro中文件读写删除与复制操作

    Win8Metro中,我们不能在向以前那样调用WIN32的API函数来进行文件操作,因此,下面就来介绍一下Win8 Metro中文件的读写操作. 1 Windows 8 Metro Style App ...

  3. WinForm中使用XML文件存储用户配置及操作本地Config配置文件(zt)

    因项目中采用CS结构读取Web.config文件,故参照一下的连接完成此功能,在此感谢原作者! 原文地址: http://www.cnblogs.com/zfanlong1314/p/3623622. ...

  4. laravel文件存储Storage

    use Illuminate\Support\Facades\Storage; //建立目录 Storage::disk('public')->makeDirectory(date('Y-m') ...

  5. Delphi阿里云对象存储OSS【支持上传文件、下载文件、删除文件、创建目录、删除目录、Bucket操作等】

    作者QQ:(648437169) 点击下载➨Delphi阿里云对象存储OSS             阿里云api文档 [Delphi阿里云对象存储OSS]支持 获取Bucket列表.设置Bucket ...

  6. Laravel 的文件存储 - Storage

    记录一下 Laravel Storage 的常见用法 内容写入磁盘文件 > php artisan tinker >>> use Illuminate\Support\Faca ...

  7. Java读取json文件并对json数据进行读取、添加、删除与修改操作

    转载:http://blog.csdn.net/qing_yun/article/details/46865863#t0   1.介绍 开发过程中经常会遇到json数据的处理,而单独对json数据进行 ...

  8. ASP FSO操作文件(复制文件、重命名文件、删除文件、替换字符串)

    ASP FSO操作文件(复制文件.重命名文件.删除文件.替换字符串)FSO的意思是FileSystemObject,即文件系统对象.FSO对象模型包含在Scripting 类型库 (Scrrun.Dl ...

  9. thinkphp对文件的上传,删除,下载操作

    工作需要,整理一下最近对php的学习经验,希望能对自己有帮助或者能帮助那些需要帮助的人. thinkphp对文件的操作,相对来说比较简单,因为tp封装好了一个上传类Upload.class.php 废 ...

随机推荐

  1. git 放弃merge 回到上一次commit

    用git reset --hard  放弃正在合并中merge,返回上一次的commit

  2. ScheduledExecutorService周期性的定时任务

    从j2se的api文档上查看ScheduledExecutorService的方法都是推迟一段时间然后相隔一段时间之后再去执行,没有想Timer定时器一样的可以在定点时间执行的api,如果也想像Tim ...

  3. elk使用不足及弥补报警措施

    全部都是6.6.2版本,就这还是没有敢选太新的 场景:每个收集点部署filebeat收集响应日志,然后发送到logstash,logstash发送到elasticsearch,和file,这里插一句, ...

  4. PyCharm-安装&调试

    windows安装pycharm 和python的链接: PyCharm:http://www.jetbrains.com/pycharm/ Python:https://www.python.org ...

  5. Discuz! ML RCE漏洞 getshell 复现

    0x01 影响版本 Discuz! ML V3.2 Discuz! ML V3.3 Discuz! ML V3.4 0x02 环境搭建 直接官网下载即可http://discuz.ml/downloa ...

  6. Mongodb的安装--简单快速

    由于需要在服务器安装mongodb,所以就对Mongodb的安装进行了了研究,在了解安装过程之前,先了解一下Mongodb,Mongodb是什么? 是什么? MongDB是结余关系数据库和非关系数据库 ...

  7. DT6.0下搜索页解决canonical获取乱码问题

    最近研究dt6.0,官方内核默认是把搜索页屏蔽的,但是做seo的人都知道,搜索页聚合是争取排名的好地方,所以我就二次开发搜索页,具体可以查看前几期分享的,今天说说关于搜索的canonical的url乱 ...

  8. el-select remote 远程搜索 多个共享一个options,options改变时输入框值不显示名称的问题

    原因:el-select remote 远程搜索 多个共享一个options,当使用 remote-method 请求数据后,options被改变,value的值不包含在options中,value的 ...

  9. 原创!ngxtop-监控nginx的利器!!!

    原创!ngxtop-监控nginx的利器!!! 无论名称还是界面,ngxtop的灵感均源自大名鼎鼎的top命令.ngxtop的功能就是,分析Nginx访问日志文件(以及其他日志文件,比如Apache2 ...

  10. SpringBoot官方文档学习(一)SpringApplication

    Springboot通过main方法启动,在许多情况下,委派给静态SpringApplication.run方法: public static void main(String[] args) { S ...