这是隐藏PHP中的文件或文件夹的正确方法

发布于 2025-02-12 11:29:24 字数 454 浏览 1 评论 0原文

我只是了解有关在PHP中使用课程的更多信息。我知道下面的代码是废话,我需要帮助。有人可以让我知道我是否朝着正确的方向前进?

while($entryName=readdir($myDirectory)) {
    $type = array("index.php", "style.css", "sorttable.js", "host-img");
    if($entryName != $type[0]){
        if($entryName != $type[1]){
            if($entryName != $type[2]){
                if($entryName != $type[3]){
                    $dirArray[]=$entryName;
                }
            }
        }
    }
}

I am just learning more about using classes in PHP. I know the code below is crap has I need help. Can someone just let me know if I am going in the right direction?

while($entryName=readdir($myDirectory)) {
    $type = array("index.php", "style.css", "sorttable.js", "host-img");
    if($entryName != $type[0]){
        if($entryName != $type[1]){
            if($entryName != $type[2]){
                if($entryName != $type[3]){
                    $dirArray[]=$entryName;
                }
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城°AllureLove 2025-02-19 11:29:24

您似乎想要的是目录中没有四个特定名称之一的所有文件的列表。

大多数类似于您的代码更有效地做到这一点是

$exclude = array("index.php", "style.css", "sorttable.js", "host-img");
$dirArray = [];

while ($entryName = readdir($myDirectory)) {
    if (!in_array($entryName, $exclude)) {
        $dirArray[] = $entryName;
    }
}

交替的,您可以在您提供的目录中分配循环(如书面,将在您提供的目录中包含文件和目录)

$exclude = array("index.php", "style.css", "sorttable.js", "host-img");
$contents = scandir($myDirectory);
$dirArray = array_diff($contents, $exclude);

以添加后续:

@arkascha有一个使用的答案array_filter,虽然该示例只是array_diff的实现,但该模式的动机是一个很好的一种:有时您可能会有时间想要排除更多的简单列表。例如,想象您要排除特定文件和所有目录是完全合理的。因此,您必须从列表中过滤目录。只是为了娱乐,我们也不要返回任何名称以开头的文件。

$exclude = ["index.php", "style.css", "sorttable.js", "host-img"];
$contents = scandir($myDirectory); // myDirectory is a valid path to the directory

$dirArray = array_filter($contents, function($fileName) use ($myDirectory, $exclude) {
    if (!in_array($fileName, $exclude) && strpos('.', $fileName) !== 0) {
        return !is_dir($myDirectory.$fileName));
    } else {
        return false;
    }
}

What you seem to want is a list of all the files in your directory that do not have one of four specific names.

The code that most resembles yours that would do it more efficiently is

$exclude = array("index.php", "style.css", "sorttable.js", "host-img");
$dirArray = [];

while ($entryName = readdir($myDirectory)) {
    if (!in_array($entryName, $exclude)) {
        $dirArray[] = $entryName;
    }
}

Alternately, you can dispense with the loop (as written, will include both files and directories in the directory you supply)

$exclude = array("index.php", "style.css", "sorttable.js", "host-img");
$contents = scandir($myDirectory);
$dirArray = array_diff($contents, $exclude);

Edit to add for posterity:

@arkascha had an answer that used array_filter, and while that example was just an implementation of array_diff, the motivation for that pattern is a good one: There may be times when you want to exclude more than just a simple list. It is entirely reasonable, for instance, to imagine you want to exclude specific files and all directories. So you have to filter directories from your list. And just for fun, let's also not return any file whose name begins with ..

$exclude = ["index.php", "style.css", "sorttable.js", "host-img"];
$contents = scandir($myDirectory); // myDirectory is a valid path to the directory

$dirArray = array_filter($contents, function($fileName) use ($myDirectory, $exclude) {
    if (!in_array($fileName, $exclude) && strpos('.', $fileName) !== 0) {
        return !is_dir($myDirectory.$fileName));
    } else {
        return false;
    }
}

﹉夏雨初晴づ 2025-02-19 11:29:24

您实际上想过滤输入:

<?php
$input = [".", "..", "folderA", "folderB", "file1", "file2", "file3"];
$blacklist = [".", "..", "folderA", "file1"];
$output = array_filter($input, function($entry) use ($blacklist) {
  return !in_array($entry, $blacklist);
});
print_r($output);

输出是:

Array
(
    [3] => folderB
    [5] => file2
    [6] => file3
)

这种方法允许实现更复杂的过滤条件,而无需多次传递输入数据。例如,如果要根据文件名称扩展名或文件创建时间(即使是在文件内容上)添加另一个过滤条件。

You actually want to filter your input:

<?php
$input = [".", "..", "folderA", "folderB", "file1", "file2", "file3"];
$blacklist = [".", "..", "folderA", "file1"];
$output = array_filter($input, function($entry) use ($blacklist) {
  return !in_array($entry, $blacklist);
});
print_r($output);

The output is:

Array
(
    [3] => folderB
    [5] => file2
    [6] => file3
)

Such approach allows to implement more complex filter conditions without having to pass over the input data multiple times. For example if you want to add another filter condition based on file name extensions or file creation time, even on file content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文