0

スクリプトファイルがあります。ディレクトリ内のファイルとフォルダーを一覧表示します。特定のファイルとフォルダーを非表示にしたい。それ、どうやったら出来るの?

<?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>
4

4 に答える 4

0

除外したいファイル名のリストを配列に入れます。

その後、ファイル名を に追加する前に、ファイル名が配列に存在する$thelistかどうかを確認します。

ファイル名がまたはif()であるかどうかをチェックするステートメントの一部として、それを追加できます。...

于 2013-09-29T05:17:32.457 に答える
0
<?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>
于 2013-09-29T05:17:34.530 に答える
0

このようなもの:

<?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>
于 2013-09-29T05:18:34.540 に答える
0

非表示にするファイル/ディレクトリの名前がわかっている場合は、そのようなエントリのセット マップを維持し、while ループ内でそれらを除外できます。

セットマップは次のようになります。

$items_to_hide = [ "/home/me/top_secret" => 1, "/home/me/passwords.txt" => 1, ... ]

And then you would modfiy your while loop like this:

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>';
    }
}

Hope this helps.

EDIT:

Also wanted to mention, using a php ordered array as your "blacklist" is quite efficient, as a single lookup will occur in almost constant time. Hence you can grow your blacklist as large as you want and still see decent performance.

于 2013-09-29T05:19:23.540 に答える