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);//删除目录
    }
    ?>
  • 相关阅读:
    frida枚举当前加载的模块以及模块中方法
    python request请求时候json严格校验怎么去除空格
    firda-so静态注册
    LeetCode 724. 寻找数组的中心索引
    LeetCode 679. 24点游戏
    LeetCode 845. 数组中的最长山脉
    并查集各种情况下的时间复杂度
    LeetCode 547. 省份数量
    LeetCode 5. 最长回文子串
    LeetCode 103. 二叉树的锯齿形层序遍历
  • 原文地址:https://www.cnblogs.com/zywf/p/5125771.html
Copyright © 2011-2022 走看看