vazo
V2EX  ›  PHP

请教如何实现跳过 update 文件夹里的 a. PHP ,其余的文件夹及文件全部删除

  •  
  •   vazo · Aug 13, 2022 · 2234 views
    This topic created in 1369 days ago, the information mentioned may be changed or developed.
    10 replies    2022-08-14 14:15:12 +08:00
    vazo
        1
    vazo  
    OP
       Aug 13, 2022
    如果是跳过文件名 a.php 的话,万一子文件夹也含有 a.php,就无法删除子文件夹.

    现在是清空 update 前复制 a.php 到外面,清空后再复制回来.
    caola
        2
    caola  
       Aug 13, 2022   ❤️ 1
    为什么一个要放这个文件到这里,不能用 nginx/apache 的 url 重写功能吗?
    vazo
        3
    vazo  
    OP
       Aug 13, 2022
    <? php
    function deldir($dir) {
    //删除目录下的文件
    $dh = opendir($dir);
    while ($file = readdir($dh)) {
    if ($file != "." && $file != "..") {
    $fullpath = $dir . "/" . $file;
    if (!is_dir($fullpath)) {
    if ($fullpath == "update/index.php") { //增加一个条件排除 update 下的 index.php
    } else {
    unlink($fullpath);
    } else {
    deldir($fullpath);
    }
    }
    }
    }
    closedir($dh);
    //删除文件夹
    if (rmdir($dir)) {
    return true;
    } else {
    return false;
    }
    }

    deldir('update') //需要清空操作的文件夹
    ?>
    vazo
        4
    vazo  
    OP
       Aug 13, 2022
    跑了一下应该是 ok 了.
    vazo
        5
    vazo  
    OP
       Aug 13, 2022
    @caola 就是喜欢 php 呀😁
    wxf666
        6
    wxf666  
       Aug 13, 2022   ❤️ 1
    『 find . -path ./update/a.php -o -delete 』?
    vazo
        7
    vazo  
    OP
       Aug 13, 2022
    @wxf666 有权限还是用 cron 简洁.
    vazo
        8
    vazo  
    OP
       Aug 13, 2022
    #3 代码有误,更正如下

    <?
    function deldir($dir) {
    //删除目录下的文件
    $dh = opendir($dir);
    while ($file = readdir($dh)) {
    if ($file != "." && $file != "..") {
    $fullpath = $dir . "/" . $file;
    if (!is_dir($fullpath)) {
    if ($fullpath == "update/a.php") { //增加一个条件排除 update 下的 a.php
    } else {
    unlink($fullpath);
    }
    } else {
    deldir($fullpath);
    }
    }
    }
    closedir($dh);
    //删除当前文件夹
    if (rmdir($dir)) {
    return true;
    } else {
    return false;
    }
    }
    deldir('update') //需要清空操作的文件夹
    ?>
    realpg
        9
    realpg  
    PRO
       Aug 13, 2022   ❤️ 1
    把 a.php 加入 git
    git reset --hard
    caola
        10
    caola  
       Aug 14, 2022
    @vazo URL 重写不知道用吗,又不是非得把这个文件放到 update 目录里才能访问和执行
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5512 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 05:47 · PVG 13:47 · LAX 22:47 · JFK 01:47
    ♥ Do have faith in what you're doing.