zoukankan      html  css  js  c++  java
  • 文件的递归与删除

    递归

    <?php
    $path="E:/php";
    
    
    function readdiguo($path,$deep=0)
    {
        $dir_handle=opendir($path);
        while(false!==$file=readdir($dir_handle))
        {
            if($file=='.' || $file=='..')continue;
            //输出该文件
            echo str_repeat('&nbsp; ',$deep*4).$file."<br />";
            //判断当前是否是目录
            if(is_dir($path.'./'.$file))
            {
                //是目录
                $func_name=__FUNCTION__;//得到当前函数名
                $func_name($path.'./'.$file,$deep+1);
            }
            
        }
    }
    readdiguo($path);
    ?>

    递归到数组

    <?php
    $path="E:/php";
    $list=readdirQiantao($path);
    foreach ($list as $first_key=>$first)
    {
        echo $first_key."=".$first["name"]."<br />";
        if($first["type"]=='file')continue;
        foreach ($first["nested"] as $two_key=>$two)
        {
            echo "&nbsp;".$two_key."=".$two["name"]."<br />";
        }
    }
    function readdirQiantao($path)
    {
        $nested=array();//存储当前目录下文件
        $dir_handle=opendir($path);
        while(false!=$file=readdir($dir_handle))
        {
            if($file=='.'||$file=="..")continue;
            
            //创建当前文件信息
            $fileinfo=array();
            $fileinfo["name"]=$file;
            
            //判断是否为目录
            if(is_dir($path.'/'.$file))
            {
                //是目录
                $fileinfo["type"]="dir";
                $func_name=__FUNCTION__;
                $fileinfo["nested"]=$func_name($path."/".$file);
            }else
            {
                //是文件
                $fileinfo["type"]="file";
            }
            //存入数组
            $nested[]=$fileinfo;
        }
        closedir($dir_handle);
        return $nested;
        
    }
    ?>

    递归删除文件

    <?php
    $path="./pp";
    var_dump(reDirs($path));
    function reDirs($path)
    {
        $dir_handle=opendir($path);
        while(false!=$file=readdir($dir_handle))
        {
            if($file=='.'||$file=='..')continue;
            
            //判断当前是否目录
            if(is_dir($path.'/'.$file))
            {
                //是目录
                $func_name=__FUNCTION__;
                $func_name($path.'/'.$file);
            }else
            {
                //是文件
                unlink($path.'/'.$file);
            }
        }
        closedir($dir_handle);
        return rmdir($path);//删除目录
    }
    ?>
  • 相关阅读:
    如何写一个简单的解释器
    linux下ifconfig, DNS以及route配置
    再次看编码
    Linux kernel API的查看
    学习Haskell的一些资料
    Unix,windows和Mac中的换行
    Cmake中的find_package功能
    知乎上有一个问题“在mfc框架中,有上面方法能直接将opencv2.0库中的Mat格式图片传递到Picture Control”中显示?
    RANSAC和Flitline
    花40分钟写一个-CBIR引擎-代码公开
  • 原文地址:https://www.cnblogs.com/zywf/p/5125771.html
Copyright © 2011-2022 走看看