eIT.com.cn 2023/3/2 12:40:39 阅读 7 次
基于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进阶之路 (2023/3/21 22:42:38)
• fpm模式下读取到is_cli为何为true (2023/3/19 19:30:13)
• 一次 Hyperf 注解失效问题分析 (2023/3/18 21:22:55)
• TP 判断IP是否在国内 (2023/3/17 23:49:51)
• windows 系统下 workerman 在同一个运行窗口中开启多个 websocket 服务 (2023/3/17 12:57:39)
• php解决缓存击穿的问题 (2023/3/16 16:49:39)
• FastAdmin的API接口生成器插件,使用validate验证时报错等问题。 (2023/3/14 6:08:24)
• php之配置和选择工具 (2023/3/10 18:09:38)
• Laravel+vue免费开源的基于RABC控制的博客系统 (2023/3/5 9:55:43)
• PHP SPL SplFileInfo FilterIterator 轮询文件删除 (2023/3/2 12:40:39)
Java编程实例 | jsoup | EasyMock | PouchDB | Exchange Server | 挺进千兆 千兆组网专题 | Powerpoint 2010入门 | 其它IT认证 | Tableau | 网管技术 | 安全测试 | 无线网配置实战专题 | QUnit | Cisco 路由技术 | SQL Server 性能调优 | 页面特效 | HSQLDB | Servlet/JSP | IMS DB | Teradata | ISA | Linux常用命令 | Macrmedia认证 | Linux软件使用 | JDBC/JDO | QlikView | mvcwea | 媒体工具 | 数据备份/恢复 | TestLink
合作媒体与友情链接 |
生活常识小贴士 | 软件开发教程 | 智慧城市生活网 | 息县通生活服务[移动版] | 息县商圈[移动版] | 美食菜谱 |
健康养生 | 法律知识 | 科技频道 | 电影影讯 | 留学考研学习 | 星座生肖|解梦说梦 |
关于我们 | 联系我们 | 合作媒体 | 使用条款 | 隐私权声明 | 版权声明 |
Copyright © 2023 eIT.com.cn. All Rights Reserved. | 豫ICP备2022012332号 |