• [PHP] visitFile()遍历指定文件夹


    visitFile()函数来源:http://topic.csdn.net/u/20071225/11/e6a8db84-df02-4622-987b-2a4ed4a5eef0.html (第14楼)

    注:visitFile()有少量修改

    <?
    // 查看指定文件夹的文件
    $fileList = array();
    function visitFile($path)
    {
    global $fileList;
    $path = str_replace("\\", "/", $path);
    $fdir = dir($path);

    while (($file = $fdir->read()) !== false)
    {
    if($file == '.' || $file == '..'){ continue; }

    $pathSub = preg_replace("*/{2,}*", "/", $path."/".$file); // 替换多个反斜杠
    $fileList[] = is_dir($pathSub) ? $pathSub."/" : $pathSub;
    if(is_dir($pathSub)){ visitFile($pathSub); }
    }
    $fdir->close();
    return $fileList;
    }
    ?>




    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <form method="get">
    <?
    $path = str_replace("\\", "/", $path);
    $path = preg_replace("*/{2,}*", "/", $path);
    ?>
    路径
    :<input type="text" name="path" id="path" value="<?=$path;?>"/><br>
    <li>磁盘根目录 /</li>
    <li>网络本地 ./phpMyAdmin</li>
    <li>本地磁盘 file://C: 或者 C:</li>
    <br>
    <input name="action" type="submit" id="action" value="view" />
    <input name="action" type="submit" id="action" value="delete" onclick="if(!confirm('是否删除 '+path.value+' 的所有子文件夹和子文件?')) return false;" />
    </form>
    <?
    if(!empty($path)){
    $path = preg_replace("*/{2,}*", "/", $path);
    $files = visitFile($path);
    switch(strtolower($_GET["action"]))
    {
    case "view":
    foreach($files as $key => $value)
    {
    printf("No.%4d&middot;%s<br>\r\n", $key+1, $value);
    }
    break;

    case "delete":
    $faileFiles = array();
    foreach(array_reverse($files) as $value)
    {
    if(!unlink($value))
    {
    array_push($faileFiles, $value);
    }
    }
              if(!unlink($path))
    {
    array_push($faileFiles, $path);
    }
    if(count($faileFiles) > 0)
    {
    printf("<br><br>删除失败文件(%d):<p>\r\n", count($faileFiles));
    foreach( $faileFiles as $key => $value)
    {
    printf("No.%4d&middot;%s<br>\r\n", $key+1, $value);
    }
    }
    break;
    }
    }
    ?>
  • 相关阅读:
    Elasticsearch5.x批量插入数据(Java)
    Elasticsearch5.x创建索引(Java)
    Kibana5.x界面简要介绍(含x-pack插件)
    CentOS6.5安装Kibana5.3.0
    Elasticsearch5.x Head插件安装
    CentOS6.5安装Elasticsearch5.3.0
    jenkins发布Spring Cloud(单机版)
    prometheus 监控docker
    gitlab迁移单个项目
    python使用pika操作rabbitmq
  • 原文地址:https://www.cnblogs.com/hcbin/p/1796425.html
Copyright © 2020-2023  润新知