Php 如何从目录中的列表中隐藏文件

Php 如何从目录中的列表中隐藏文件,php,Php,我有一个脚本文件。列出目录中的文件和文件夹。。我想隐藏某些文件和文件夹。我该怎么做 <?php if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if (($file != ".") && ($file != "..")) { $thelist .= '<LI><

我有一个脚本文件。列出目录中的文件和文件夹。。我想隐藏某些文件和文件夹。我该怎么做

<?php
if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle)))
    {
        if (($file != ".") 
         && ($file != ".."))
        {
            $thelist .= '<LI><a href="'.$file.'">'.$file.'</a>';
        }
    }

    closedir($handle);
}
?>

<P>List of files:</p>
<UL>
<P><?=$thelist?></p>
</UL>

文件列表:


将要排除的文件名列表放入数组中

然后,在将文件名添加到
$thelist
之前检查文件名是否正确

您可以将其添加为检查文件名是
还是
if()
语句的一部分。


<?php
$files_to_hide = array('file1.txt', 'file2.txt');
if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle)))
    {
        if (($file != ".") && ($file != "..") && !in_array($file, $files_to_hide))
        {
            $thelist .= '<LI><a href="'.$file.'">'.$file.'</a>';
        }
    }

    closedir($handle);
}
?>

<P>List of files:</p>
<UL>
<P><?=$thelist?></p>
</UL>

文件列表:

类似这样的内容:

<?php
$bannedFiles = Array(".", "..", "example");
if ($handle = opendir('.')){
    while (false !== ($file = readdir($handle)))
    {
        $banned = false;
        foreach ($bannedFiles as $bFile){
            if ($bFile == $file){
                $banned = true;
            }
        }
        if (!$banned){
            $thelist .= '<LI><a href="'.$file.'">'.$file.'</a></LI>';
        }
    }

    closedir($handle);
}
?>

<P>List of files:</p>
<UL>
<P><? echo $thelist;?></p>
</UL>
$items_to_hide = [ "/home/me/top_secret" => 1, "/home/me/passwords.txt" => 1, ... ]
while (false !== ($file = readdir($handle)))
{
    // check map if said file is supposed to be hidden, if so skip current loop iteration
    if($items_to_hide[$file]) {
      continue;
    }
    if (($file != ".") 
     && ($file != ".."))
    {
        $thelist .= '<LI><a href="'.$file.'">'.$file.'</a>';
    }
}

文件列表:


如果知道要隐藏的文件/目录的名称,可以维护这些条目的一组映射,并在while循环中过滤掉它们

您的集合贴图如下所示:

<?php
$bannedFiles = Array(".", "..", "example");
if ($handle = opendir('.')){
    while (false !== ($file = readdir($handle)))
    {
        $banned = false;
        foreach ($bannedFiles as $bFile){
            if ($bFile == $file){
                $banned = true;
            }
        }
        if (!$banned){
            $thelist .= '<LI><a href="'.$file.'">'.$file.'</a></LI>';
        }
    }

    closedir($handle);
}
?>

<P>List of files:</p>
<UL>
<P><? echo $thelist;?></p>
</UL>
$items_to_hide = [ "/home/me/top_secret" => 1, "/home/me/passwords.txt" => 1, ... ]
while (false !== ($file = readdir($handle)))
{
    // check map if said file is supposed to be hidden, if so skip current loop iteration
    if($items_to_hide[$file]) {
      continue;
    }
    if (($file != ".") 
     && ($file != ".."))
    {
        $thelist .= '<LI><a href="'.$file.'">'.$file.'</a>';
    }
}
然后你会像这样修改你的while循环:

<?php
$bannedFiles = Array(".", "..", "example");
if ($handle = opendir('.')){
    while (false !== ($file = readdir($handle)))
    {
        $banned = false;
        foreach ($bannedFiles as $bFile){
            if ($bFile == $file){
                $banned = true;
            }
        }
        if (!$banned){
            $thelist .= '<LI><a href="'.$file.'">'.$file.'</a></LI>';
        }
    }

    closedir($handle);
}
?>

<P>List of files:</p>
<UL>
<P><? echo $thelist;?></p>
</UL>
$items_to_hide = [ "/home/me/top_secret" => 1, "/home/me/passwords.txt" => 1, ... ]
while (false !== ($file = readdir($handle)))
{
    // check map if said file is supposed to be hidden, if so skip current loop iteration
    if($items_to_hide[$file]) {
      continue;
    }
    if (($file != ".") 
     && ($file != ".."))
    {
        $thelist .= '<LI><a href="'.$file.'">'.$file.'</a>';
    }
}
while(false!=($file=readdir($handle)))
{
//若所述文件被认为是隐藏的,则检查映射,若是,则跳过当前循环迭代
如果($items\u to\u hide[$file]){
继续;
}
如果(($file!=”)
&&($file!=“.”)
{
$thelist.='LI>';
}
}
希望这有帮助

编辑:


还想提到的是,使用php有序数组作为“黑名单”是非常有效的,因为单个查找将在几乎恒定的时间内进行。因此,您可以将您的黑名单扩大到您想要的大小,并且仍然可以看到良好的性能。

您事先知道文件的名称吗?是否有任何特定原因不添加
”。“
$file\u to\u hide
数组中?