基于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(), //是否可写
标签:文件,SplFileInfo,获取,轮询,getCTime,item,SPL,file,PHP From: https://www.cnblogs.com/phpnan/p/17171252.html