Possible Duplicates:
Get the Files inside a directory
PHP: scandir() is too slow可能重复:获取目录中的文件PHP:scandir()太慢了
I have a directory with tens of thousands of files in it and I want to display a list of these files on a page. I tried doing it with scandir and it takes forever. What would be an efficient method of achieving this?
我有一个包含数万个文件的目录,我想在页面上显示这些文件的列表。我尝试用scandir做它,它需要永远。实现这一目标的有效方法是什么?
3 个解决方案
#1
8
I recommend using DirectoryIterator or RecursiveDirectoryIterator.
我建议使用DirectoryIterator或RecursiveDirectoryIterator。
#2
1
I haven't benchmarked them, but your other options are
我没有对他们进行基准测试,但你的其他选择是
glob() - http://php.net/manual/en/function.glob.php
glob() - http://php.net/manual/en/function.glob.php
opendir() - http://www.php.net/manual/en/function.opendir.php
opendir() - http://www.php.net/manual/en/function.opendir.php
#3
0
$directory=opendir($_SERVER['DOCUMENT_ROOT'].'/directory/');
while ($file = readdir($directory)) {
if($file!="." && $file!=".."){
echo $file."<br>";
}
}
closedir($directory);
#1
8
I recommend using DirectoryIterator or RecursiveDirectoryIterator.
我建议使用DirectoryIterator或RecursiveDirectoryIterator。
#2
1
I haven't benchmarked them, but your other options are
我没有对他们进行基准测试,但你的其他选择是
glob() - http://php.net/manual/en/function.glob.php
glob() - http://php.net/manual/en/function.glob.php
opendir() - http://www.php.net/manual/en/function.opendir.php
opendir() - http://www.php.net/manual/en/function.opendir.php
#3
0
$directory=opendir($_SERVER['DOCUMENT_ROOT'].'/directory/');
while ($file = readdir($directory)) {
if($file!="." && $file!=".."){
echo $file."<br>";
}
}
closedir($directory);