PHP遍历文件目录与清除目录中的文件

巴扎黑
巴扎黑 原创
2016-11-24 13:53:20 825浏览

今天无聊中练习了一下PHP遍历文件目录的程序,编写了以下两个程序,不过质量不是很好,轻拍~~~

1、清除PHP缓存文件

<?php
function read_dir($dir,$file)
{
$a =strpos($file,".php");
if($a>0) 
{
unlink($dir . $file);
echo "delete $dir$file <br>";
return true;
}
if(strpos($file,".") === 0 || strpos($file,".") !== false ) return true;
if(strpos($file,".") === false || strpos($dir,"/") === false) 
{
$dir = $dir . $file . "/";
if(!is_dir($dir)) return false;
$dh = opendir($dir);
while(($file = readdir($dh)) != false)
{
read_dir($dir,$file);   //递归调用
}
}
}
function clear_caches()
{
$dir = "./temp/";  //要清除的PHP缓存文件目录
if(!is_dir($dir)) die("It is not a dir");
$dh = opendir($dir);
while(($file = readdir($dh) )!=false)
{
//var_dump($file);
read_dir($dir,$file);
}
}
?>


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。