搜索具有特定名称的txt文件,然后使用PHP删除它们?

搜索具有特定名称的txt文件,然后使用PHP删除它们?,php,html,recursion,directory,unlink,Php,Html,Recursion,Directory,Unlink,使用php中的unlink函数,可以在包含多个文件夹的目录中搜索具有特定名称的txt文件。在我的例子中,Newsfeed.txt 从哪里开始?您可以使用php标准库(SPL)的递归目录迭代器 这将允许您从给定文件夹和所有子文件夹中删除名为Newsfeed.txt的所有文件。您可以使用php标准库(SPL)的递归目录迭代器 这将允许您从给定文件夹和所有子文件夹中删除名为Newsfeed.txt的所有文件。Great answer maxhb。这里有一些更为手动的东西 <?php funct

使用php中的unlink函数,可以在包含多个文件夹的目录中搜索具有特定名称的txt文件。在我的例子中,
Newsfeed.txt


从哪里开始?

您可以使用php标准库(SPL)的递归目录迭代器


这将允许您从给定文件夹和所有子文件夹中删除名为
Newsfeed.txt的所有文件。

您可以使用php标准库(SPL)的递归目录迭代器


这将允许您从给定文件夹和所有子文件夹中删除名为
Newsfeed.txt的所有文件。

Great answer maxhb。这里有一些更为手动的东西

<?php

function unlink_newsfeed($checkThisPath) {
    $undesiredFileName = 'Newsfeed.txt';

    foreach(scandir($checkThisPath) as $path) {
        if (preg_match('/^(\.|\.\.)$/', $path)) {
            continue;
        }

        if (is_dir("$checkThisPath/$path")) {
            unlink_newsfeed("$checkThisPath/$path");
        } else if (preg_match( "/$undesiredFileName$/", $path)) {
            unlink("$checkThisPath/$path");
        }
    }
}

unlink_newsfeed(__DIR__);

答案很好,maxhb。这里有一些更为手动的东西

<?php

function unlink_newsfeed($checkThisPath) {
    $undesiredFileName = 'Newsfeed.txt';

    foreach(scandir($checkThisPath) as $path) {
        if (preg_match('/^(\.|\.\.)$/', $path)) {
            continue;
        }

        if (is_dir("$checkThisPath/$path")) {
            unlink_newsfeed("$checkThisPath/$path");
        } else if (preg_match( "/$undesiredFileName$/", $path)) {
            unlink("$checkThisPath/$path");
        }
    }
}

unlink_newsfeed(__DIR__);

获取目录中的所有文件名:-。现在使用foreach检查文件名并删除它们。获取目录中的所有文件名:-。现在使用foreach检查文件名并删除它们。好的,我应该解释一下php对我来说是非常新的。因此,第一部分搜索一个集合目录,并将所有结果放入$diritGenerator中。我明白了。但我不知道如何在$dirIterator中搜索名为Newsfeed.txt的文件?把它们都拆开?。Sorry编辑示例以满足您的需求。好的,我应该解释一下php对我来说是非常新的。因此,第一部分搜索一个集合目录,并将所有结果放入$diritGenerator中。我明白了。但我不知道如何在$dirIterator中搜索名为Newsfeed.txt的文件?把它们都拆开?。Sorry已编辑示例以满足您的要求。