laravel 学习3 文件存储

Cache 缓存 view 在Storage 下面会自动生成对应的文件。但是并不会自动删除缓存文件,所有今天就想着看看框架底层,后来发现,有一个 path方法可以获取  对应的 文件目录地址,因此找到解决办法。废话少说,下面贴出代码。

 

 /**
     *  清除 缓存文件
     *  f y j
     *  
     */
    public static function deleteStoreFile($file)
    {
        $filedata = explode('/', $file);

        $num = count($filedata);
        $newfile_tmp= '';
        $newfile = '';
        foreach ($filedata as $key => $value) {
            if(($key+2)<=$num)
            {
                $newfile .=empty($key)?$value:'/'.$value;
            }

            if(($key+1)<=$num-2)
            {
                $newfile_tmp .=empty($key)?$value:'/'.$value;
            }
        }
        if (file_exists($file)) {
            Util::deldir($newfile);
            rmdir($newfile_tmp);
        }
    }

    /**
     *  清空 文件夹下面所有 子文件夹
     * @param  [type] $dir [description]
     * @return [type]      [description]
     */
    public static function deldir($dir) {
          //先删除目录下的文件:
          $dh=opendir($dir);
          while ($file=readdir($dh)) {
            if($file!="." && $file!="..") {
              $fullpath=$dir."/".$file;

              if(!is_dir($fullpath)) {
                  unlink($fullpath);
              } else {
                  Util::deldir($fullpath);
              }
            }
          }
          closedir($dh);
          if(rmdir($dir)) {
            return true;
          }
          return false;
    }
把对应的 path 重写一些,因为是私有的额,外部访问不到。

 /**
     * 获取缓存文件跟路径
     * fyj
     */
    public function path($key)
    {
        $storagepath = storage_path('framework/cache');
        $parts = array_slice(str_split($hash = sha1($key), 2), 0, 2);

        return $storagepath.'/'.implode('/', $parts).'/'.$hash;
    }
以下是删除代码调用
$file = $this->path(config('msgconfig.NEWS_SHOW_CACHE').$id);
            if(file_exists($file))
            {
                Util::deleteStoreFile($file);
            }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值