Home  >  Article  >  Backend Development  >  PHP traverses file directories and clears files in directories

PHP traverses file directories and clears files in directories

巴扎黑
巴扎黑Original
2016-11-24 13:53:201104browse

Today I was bored and practiced the PHP program for traversing file directories, and wrote the following two programs, but the quality is not very good, pat~~~

1. Clear PHP cache files

<?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);
}
}
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn