V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
vazo
V2EX  ›  PHP

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

  •  
  •   vazo · 2022-08-13 10:16:58 +08:00 · 1548 次点击
    这是一个创建于 593 天前的主题,其中的信息可能已经有所发展或是发生改变。
    10 条回复    2022-08-14 14:15:12 +08:00
    vazo
        1
    vazo  
    OP
       2022-08-13 11:06:52 +08:00
    如果是跳过文件名 a.php 的话,万一子文件夹也含有 a.php,就无法删除子文件夹.

    现在是清空 update 前复制 a.php 到外面,清空后再复制回来.
    caola
        2
    caola  
       2022-08-13 12:23:17 +08:00   ❤️ 1
    为什么一个要放这个文件到这里,不能用 nginx/apache 的 url 重写功能吗?
    vazo
        3
    vazo  
    OP
       2022-08-13 12:36:05 +08:00
    <? 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
       2022-08-13 12:37:02 +08:00
    跑了一下应该是 ok 了.
    vazo
        5
    vazo  
    OP
       2022-08-13 12:38:09 +08:00
    @caola 就是喜欢 php 呀😁
    wxf666
        6
    wxf666  
       2022-08-13 12:39:22 +08:00   ❤️ 1
    『 find . -path ./update/a.php -o -delete 』?
    vazo
        7
    vazo  
    OP
       2022-08-13 12:48:17 +08:00
    @wxf666 有权限还是用 cron 简洁.
    vazo
        8
    vazo  
    OP
       2022-08-13 13:25:55 +08:00
    #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  
       2022-08-13 15:23:41 +08:00   ❤️ 1
    把 a.php 加入 git
    git reset --hard
    caola
        10
    caola  
       2022-08-14 14:15:12 +08:00
    @vazo URL 重写不知道用吗,又不是非得把这个文件放到 update 目录里才能访问和执行
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5247 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 09:37 · PVG 17:37 · LAX 02:37 · JFK 05:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.