zoukankan      html  css  js  c++  java
  • Laravel 的文件存储

    记录一下 Laravel Storage 的常见用法

    内容写入磁盘文件

    > php artisan tinker
    >>> use IlluminateSupportFacadesStorage;
    >>> Storage::put('test.txt', 'hello');
    => true
    
    ls storage/app/
    public/  test.txt
    

    文件默认创建在 /storage/app 目录下

    获取文件存储的本地磁盘全路径

    继续在 tinker 中测试一下

    >>> storage_path()
    => "/home/zhongwei/work/my_project/storage"
    >>> storage_path('test.txt')
    => "/home/zhongwei/work/my_project/storage/test.txt"
    >>> storage_path('app/test.txt')
    => "/home/zhongwei/work/my_project/storage/app/test.txt"
    

    可见,应该是 storage_path('app/test.txt')

    删除文件

    >>> Storage::delete('test.txt')
    => true
    

    文件默认存储路径是在哪里设置的

    Config/filesystems.php

        'disks' => [
    
            'local' => [
                'driver' => 'local',
                'root' => storage_path('app'),
            ],
    
            'public' => [
                'driver' => 'local',
                'root' => storage_path('app/public'),
                'url' => env('APP_URL').'/storage',
                'visibility' => 'public',
            ],
    

    Local.root 指定的即是默认路径。

    判断一个文件是否存在

    >>> Storage::put('public/test.txt', 'hello');
    => true
    >>> Storage::exists('public/test.txt');
    => true
    >>> Storage::exists('public/test1.txt');
    => false
  • 相关阅读:
    structs2---OGNL表达式
    六种获取配置properties文件的方法
    java poi导出Excel 总结
    Linux中发布项目的一些命令笔记
    JavaScript 闭包
    常见数据库连接方式
    Docker(五):镜像
    Docker(四):docker的安装
    Ubuntu命令
    Docker(三):Docker的基本概念
  • 原文地址:https://www.cnblogs.com/sgm4231/p/10194826.html
Copyright © 2011-2022 走看看