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

yipeiwu_com5年前PHP代码库
今天无聊中练习了一下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/"; 
    $dir = "c:/"; 
    $up_dir = "上级目录"; 
    $up_url = $dir; 

    if(isset($_REQUEST['act']) && $_REQUEST['act']=='list_dir') 
    { 
        if(emptyempty($_REQUEST['dir'])) 
        { 
            $up_dir="目录为空!"; 
        } 
        $dir = isset($_REQUEST['dir']) ? $_REQUEST['dir'] : $dir; 

    } 
    if(!is_dir($dir)) 
    { 
        $up_dir="无效目录!"; 
    } 

    ?> 

        <tr> 
            <td colspan="3"> 
            <?php  

            if(strpos($up_dir,"上级目录")!==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> 

 
</html> 

相关文章

php生成txt文件实例代码介绍

这是一个朋友过来的 php 生成 txt 文件代码,这只是一个实例,需要我来给他生成多个 txt 文件实例的,但我觉得他这个代码有点意思,所以就分享上来了。 先说下这个 php 生成 t...

PHP中overload与override的区别

override(重写,覆盖) 1、方法名、参数、返回值相同。 2、子类方法不能缩小父类方法的访问权限。 3、子类方法不能抛出比父类方法更多的异常(但子类方法可以不抛出异常)。 4、存...

PHP实现简单数字分页效果

学习要点: 1.LIMIT 用法 2.各种参数 3.超链接调用 第一:先在文件中设置数字分页模块;我的文件是(blog.php) 复制代码 代码如下://分页模块 $_page = $_...

PHP array_multisort() 函数的深入解析

一、先看最简单的情况。有两个数组:$arr1 = array(1,9,5);$arr2 = array(6,2,4);array_multisort($arr1,$arr2);print...

PHP PDO函数库(PDO Functions)第1/2页

与ADODB和MDB2相比,PDO更高效。目前而言,实现“数据库抽象层”任重而道远,使用PDO这样的“数据库访问抽象层”是一个不错的选择。 PDO->beginTransactio...