【发布时间】:2012-01-09 08:16:29
【问题描述】:
我遇到了几篇关于性能和 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 快,但我想知道这是否是一个好习惯?
非常感谢
【问题讨论】:
-
系统调用肯定是不可移植的。您的示例代码依赖于 *nix 操作系统。
-
我有 apache 和 php+mysql 的 centos 5 可以吗?
-
习惯使用
system()调用也是一个坏主意。没有参数它们应该没问题,但如果你根据用户输入动态构造它们,你可能会造成严重的安全漏洞。 -
@Inerdial 哦,我没想到,谢谢