我遇到了几篇关于性能和 readdir 的文章,这里是 php 脚本:
function getDirectory( $path = '.', $level = 0 ) {
$ignore = array( 'cgi-bin', '.', '..' );
$dh = @opendir( $path );
while( false !== ( $file = readdir( $dh ) ) ){
if( !in_array( $file, $ignore ) ){
$spaces = str_repeat( ' ', ( $level * 4 ) );
if( is_dir( "$path/$file" ) ){
echo "$spaces $file\n";
getDirectory( "$path/$file", ($level+1) );
} else {
echo "$spaces $file\n";
}
}
}
closedir( $dh );
}
getDirectory( "." );
这会正确地回显文件/文件夹。
现在我发现了这个:
$t = system('find');
print_r($t);
它还可以找到所有文件夹和文件,然后我可以像第一个代码一样创建一个数组。
我认为这system('find');
比那个快,readdir
但我想知道这是否是一个好习惯?非常感谢你