哪種方法或其他方法最適合顯示文件?
注意:在這個例子中,我只對顯示文件名感興趣
另外,FilesystemIterator
中的項目如何排序?
以下三個示例都顯示相同的結果,但FilesystemIterator
沒有明顯的排序順序。exec()vs glob()vs FilesystemIterator()
$path = "/";
exec("ls $path", $results);
foreach($results as $file){
p($file);
}
foreach(glob($path."/*") as $file){
p(basename($file) );
}
foreach(new FilesystemIterator($path) as $file){
p($file->getFilename());
}
function p($s){
global $path;
echo "<a href=\"$path?f=$s\">$s</a><BR>\n";
}
輸出:
exec("ls ...") method
bin
boot
cdrom
dev
etc
home
initrd.img
...
glob() method
bin
boot
cdrom
dev
etc
home
initrd.img
...
FilesystemIterator() method
mnt
vmlinuz
cdrom
usr
sys
home
var
...
這與FilesystemIterator的輸出完全相同,只不過有點 – ElefantPhace
它不是,FilesystemIterator返回SplFileObject的集合 – apocalypz
至於輸出**只有**文件名,正如我所做的那樣,沒有什麼不同 – ElefantPhace