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

来源:互联网 发布:激活软件下载 编辑:程序博客网 时间:2024/05/17 23:55
PHP文件目录遍历

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

 

1、清除PHP缓存文件

 

 

Php代码  收藏代码
  1. <?php  
  2.   
  3. function read_dir($dir,$file)  
  4. {  
  5.     $a =strpos($file,".php");  
  6.       
  7.     if($a>0)   
  8.     {  
  9.         unlink($dir . $file);  
  10.         echo "delete $dir$file <br>";  
  11.         return true;  
  12.     }  
  13.       
  14.     if(strpos($file,".") === 0 || strpos($file,".") !== false ) return true;  
  15.       
  16.     if(strpos($file,".") === false || strpos($dir,"/") === false)   
  17.     {  
  18.         $dir = $dir . $file . "/";  
  19.         if(!is_dir($dir)) return false;  
  20.         $dh = opendir($dir);  
  21.         while(($file = readdir($dh)) != false)  
  22.         {  
  23.             read_dir($dir,$file);   //递归调用  
  24.         }  
  25.     }  
  26. }  
  27.   
  28. function clear_caches()  
  29. {  
  30.     $dir = "./temp/";  //要清除的PHP缓存文件目录  
  31.   
  32.     if(!is_dir($dir)) die("It is not a dir");  
  33.     $dh = opendir($dir);  
  34.   
  35.     while(($file = readdir($dh) )!=false)  
  36.     {  
  37.         //var_dump($file);  
  38.         read_dir($dir,$file);  
  39.   
  40.     }  
  41. }  
  42.   
  43.   
  44. ?>  

 

 

2、遍历目录中所有文件

 

Php代码  收藏代码
  1. <html>  
  2.   
  3. <head>  
  4.     <meta http-enquiv="Content-Type" content="text/html;charset=gb2312">  
  5.     <title>查看目录</title>  
  6. </head>  
  7.   
  8. <body>  
  9.     <table width="600" align="center">  
  10.         <tr>  
  11.             <th width="50%">文件名</th>  
  12.             <th width="25%">修改时间</th>  
  13.             <th width="25%">文件大小(k)</th>  
  14.         </tr>  
  15.   
  16.   
  17.     <?php  
  18.        
  19.     //$dir = "./admin/";  
  20.     $dir = "c:/";  
  21.     $up_dir = "上级目录";  
  22.     $up_url = $dir;  
  23.       
  24.     if(isset($_REQUEST['act']) && $_REQUEST['act']=='list_dir')  
  25.     {  
  26.         if(emptyempty($_REQUEST['dir']))  
  27.         {  
  28.             $up_dir="目录为空!";  
  29.         }  
  30.         $dir = isset($_REQUEST['dir']) ? $_REQUEST['dir'] : $dir;  
  31.   
  32.     }  
  33.     if(!is_dir($dir))  
  34.     {  
  35.         $up_dir="无效目录!";  
  36.     }  
  37.   
  38.     ?>  
  39.   
  40.         <tr>  
  41.             <td colspan="3">  
  42.             <?php   
  43.               
  44.             if(strpos($up_dir,"上级目录")!==false)  
  45.             {  
  46.                 //if($up_url=="") echo $up_dir;  
  47.                 if($dir != "./admin/")  
  48.                 {  
  49.                  $up_url = substr($dir,0,-1);  
  50.                  $k = strrpos($up_url,"/");  
  51.                  $up_url = substr($up_url,0,$k-strlen($up_url));  
  52.                  $up_url = $up_url ."/";  
  53.                 }  
  54.                  echo "<a href=\"test.php?act=list_dir&dir=$up_url\">$up_dir</a>";  
  55.             }   
  56.             else   
  57.             {  
  58.                 echo $up_dir;  
  59.                 die();  
  60.             }  
  61.             ?>  
  62.             </td>  
  63.         </tr>  
  64.     <?php   
  65.         $up_dir = $dir;  
  66.         $dh = opendir($dir);    
  67.         while(($file=readdir($dh)) != false)  
  68.         {     
  69.             if($file != "." && $file != ".." && $file != ".svn" )  
  70.             {  
  71.                 if(strpos($file,".") !==false)  
  72.                 {  
  73.                     $time = date("Y-m-d H:i:s"filectime($dir . $file));  
  74.                     $size = filesize($dir . $file)/1000;  
  75.                     echo "<tr><td>$file</td><td>$time</td><td>$size</td></tr>";  
  76.                 }  
  77.                 else  
  78.                 {     
  79.                     $time = date("Y-m-d H:i:s."filectime($dir . $file));  
  80.                     $size = filesize($dir . $file)/1000;  
  81.                     $dir = $dir . $file ."/";  
  82.                       
  83.                     echo "<tr><td><a href =\"test.php?act=list_dir&dir=$dir\">$file</a></td><td>$time</td><td>$size</td></tr>";  
  84.                     $dir = $up_dir;  
  85.                       
  86.                 }  
  87.             }  
  88.         }  
  89.           
  90.     ?>  
  91.           
  92.   
  93.     </table>  
  94.   
  95. <?php die();  ?>  
  96. </body>  
  97.   
  98.   
  99. </html> 

原创粉丝点击