我想使用 opendir() 仅列出特定文件夹(即/www/site/)中的文件夹。我想从列表中排除文件以及“。”和出现在 linux 文件夹列表中的“..”文件夹。我该怎么做呢?
最佳答案
查看 PHP docs for readdir() .它包括一个例子。
为了完整性:
<?php
if ($handle = opendir('.')) {
$blacklist = array('.', '..', 'somedir', 'somefile.php');
while (false !== ($file = readdir($handle))) {
if (!in_array($file, $blacklist)) {
echo "$file\n";
}
}
closedir($handle);
}
?>
只需将 opendir('.')
更改为您的目录,即 opendir('/www/sites/')
,并更新 $blacklist
来包含您不想输出的文件或目录的名称。
关于PHP opendir() 仅列出文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6497833/