首页 > 开发 > PHP > 正文

使用PHP遍历文件目录与清除目录中文件的实现详解

2024-05-04 22:25:24
字体:
来源:转载
供稿:网友
今天无聊中练习了一下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); 

    } 


 
?> 

 2、遍历目录中所有文件
代码如下:
<html> 

<head> 
    <meta http-enquiv="Content-Type" content="text/html;charset=gb2312"> 
    <title>查看目录</title> 
</head> 

<body> 
    <table width="600" align="center"> 
        <tr> 
            <th width="50%">文件名</th> 
            <th width="25%">修改时间</th> 
            <th width="25%">文件大小(k)</th> 
        </tr> 

 
    <?php 

    //$dir = "./admin/"; 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表