Today bored to practice the PHP traversal file directory program, wrote the following two programs, but the quality is not very good, Pat ~ ~ ~ ~
1, clear PHP cache files
Copy Code code as follows:
<?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); Recursive call
}
}
}
function Clear_caches ()
{
$dir = "./temp/"; Directory of PHP cache files to erase
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);
}
}
?>
2, traversing all the files in the directory
Copy Code code as follows:
<meta http-enquiv= "Content-type" content= "text/html;charset=gb2312" >
<title> View Catalog </title>
<body>
<table width= "align=" "Center" >
<tr>
<th width= "50%" > FileName </th>
<th width= "25%" > Modification Time </th>
<th width= "25%" > File size (k) </th>
</tr>
<?php
$dir = "./admin/";
$dir = "c:/";
$up _dir = "Superior catalogue";
$up _url = $dir;
if (Isset ($_request[' act ')) && $_request[' act ']== ' List_dir ')
{
if (Emptyempty ($_request[' dir '))
{
$up _dir= "Directory is empty! ";
}
$dir = isset ($_request[' dir '])? $_request[' dir ']: $dir;
}
if (!is_dir ($dir))
{
$up _dir= "Invalid directory! ";
}
?>
<tr>
<TD colspan= "3" >
<?php
if (Strpos ($up _dir, "parent directory")!==false)
{
if ($up _url== "") echo $up _dir;
if ($dir!= "./admin/")
{
$up _url = substr ($dir, 0,-1);
$k = Strrpos ($up _url, "/");
$up _url = substr ($up _url,0, $k-strlen ($up _url));
$up _url = $up _url. " /";
}
echo "<a href=\" test.php?act=list_dir&dir= $up _url\ "> $up _dir</a>";
}
Else
{
echo $up _dir;
Die ();
}
?>
</td>
</tr>
<?php
$up _dir = $dir;
$DH = Opendir ($dir);
while (($file =readdir ($DH))!= false)
{
if ($file!= "." && $file!= "..." && $file!= ". SVN")
{
if (Strpos ($file, ".")! ==false)
{
$time = Date ("y-m-d h:i:s", Filectime ($dir. $file));
$size = FileSize ($dir. $file)/1000;
echo "<tr><td> $file </td><td> $time </td><td> $size </td></tr>";
}
Else
{
$time = Date ("Y-m-d h:i:s.", Filectime ($dir. $file));
$size = FileSize ($dir. $file)/1000;
$dir = $dir. $file. " /";
echo "<tr><td><a href =\" test.php?act=list_dir&dir= $dir \ "> $file </a></td><td > $time </td><td> $size </td></tr> ";
$dir = $up _dir;
}
}
}
?>
</table>
<?php die (); ?>
</body>