PHP opendir()仅列出文件夹 [英] PHP opendir() to list folders only
问题描述
我想使用opendir()仅列出特定文件夹(即/www/site/)中的文件夹.我也想从列表中排除文件.和出现在linux文件夹列表中的".."文件夹.我将如何去做?
I would like to use opendir() to list only the folders in a particular folder (i.e. /www/site/). I would like to exclude files from the list as well at the '.' and '..' folders that appear on a linux folder listing. How would I go about doing this?
推荐答案
查看 PHP文档中的readdir().它包括一个确切的例子.
Check out the PHP docs for readdir(). It includes an example for exactly this.
出于完整性考虑:
<?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
以包括您不希望输出的文件或目录的名称.
Simply change opendir('.')
to your directory, i.e. opendir('/www/sites/')
, and update $blacklist
to include the names of files or directory you do not wish to output.
这篇关于PHP opendir()仅列出文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!