设为首页 加入收藏

TOP

PHP SPL SplFileInfo FilterIterator 轮询文件删除
2023-07-23 13:26:13 】 浏览:68
Tags:PHP SPL SplFileInfo FilterIterator 文件删

基于PHP spl 遍历文件删除过期的日志文件

 

一.定义PHP类 , 限制文件扩展 RecursiveFileFilterIterator.class.php 

<?php
class RecursiveFileFilterIterator  extends FilterIterator 
{
	
	protected $ext = array('log','jpg','gif');

	public function __construct($path) {
		parent::__construct(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)));
	}

	public function accept() {
		$item = $this->getInnerIterator();
		if ($item->isFile() && in_array(pathinfo($item->getFilename(), PATHINFO_EXTENSION), $this->ext)) {
			return TRUE;
		}
	}
}

  

二.调用 

<?php 
require 'RecursiveFileFilterIterator.class.php';
try {
    foreach (new RecursiveFileFilterIterator("d:\xxx\xxx\log") as $item) {
        echo "=====================".PHP_EOL;
        if (time() - $item->getCTime() > (24*3600*30)) {
            var_dump($item);
            var_dump($item->getPathName());
            #@unlink($item->getPathName());
        }
    }
} catch (Exception $e) {
    var_dump($e->getMessage());
}

 

三.附上网上整理的一些方法和属性 

 'getATime' => $file->getATime(), //最后访问时间
    'getBasename' => $file->getBasename(), //获取无路径的basename
    'getCTime' => $file->getCTime(), //获取inode修改时间
    'getExtension' => $file->getExtension(), //文件扩展名
    'getFilename' => $file->getFilename(), //获取文件名
    'getGroup' => $file->getGroup(), //获取文件组
    'getInode' => $file->getInode(), //获取文件inode
    'getLinkTarget' => $file->getLinkTarget(), //获取文件链接目标文件
    'getMTime' => $file->getMTime(), //获取最后修改时间
    'getOwner' => $file->getOwner(), //文件拥有者
    'getPath' => $file->getPath(), //不带文件名的文件路径
    'getPathInfo' => $file->getPathInfo(), //上级路径的SplFileInfo对象
    'getPathname' => $file->getPathname(), //全路径
    'getPerms' => $file->getPerms(), //文件权限
    'getRealPath' => $file->getRealPath(), //文件绝对路径
    'getSize' => $file->getSize(),//文件大小,单位字节
    'getType' => $file->getType(),//文件类型 file  dir  link
    'isDir' => $file->isDir(), //是否是目录
    'isFile' => $file->isFile(), //是否是文件
    'isLink' => $file->isLink(), //是否是快捷链接
    'isExecutable' => $file->isExecutable(), //是否可执行
    'isReadable' => $file->isReadable(), //是否可读
    'isWritable' => $file->isWritable(), //是否可写

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇浅谈PHP设计模式的享元模式 下一篇学习swoole之前,你需要知道的几..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目