Using PHP to traverse the file directory and clear the implementation of files in the directory _php tips

Source: Internet
Author: User
Tags parent directory
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>
&LT;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>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.