Php 使用限制文件数扫描目录?

Php 使用限制文件数扫描目录?,php,directory,Php,Directory,我已经编写了一些代码来打印文件夹中带有限制和偏移量的文件(比如MySQL的limit) 我的代码: /* files: . .. read.txt */ $dir = "lab"; $limit = 2; $file_id = 1; $start = 1; $files = array(); $dh = opendir($dir); while (false !== ($filename = readdir($dh))) { if($file_id > $start and $f

我已经编写了一些代码来打印文件夹中带有限制和偏移量的文件(比如MySQL的
limit

我的代码:

/*
files:
.
..
read.txt
*/
$dir = "lab";
$limit = 2;
$file_id = 1;
$start = 1;
$files = array();
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
    if($file_id > $start and $file_id >= $limit){
        $files[$file_id] = $filename;
    }
    $file_id++;
}   
print_r($files);
/*
files:
Array ( [2] => .. [3] => read.txt ) 
*/
“我的文件夹”可能包含1000个或更多文件,每个访问者都将执行此功能


所以我想在没有循环的情况下完成这项工作,这可能吗?如果不是,有什么方法可以加快速度吗?

您可以检索目录中的整个文件列表,而无需使用任何循环

检索完一系列文件后,您可以应用任何类型的筛选(使用)和限制(使用)

可能是避免自己循环的最佳方法

如果您愿意,还可以实现这样的基本启动/限制系统

$start = 5;
$limit = 20;

$thisFile = 0;

while (false !== ($filename = readdir($dh))) {
    // Check the start
    if ($thisFile < $start)
        continue;
    $thisFile++;

    // Check the end
    if ($thisFile > $limit)
        break;

    // Your other code can go in here
}
$start=5;
$limit=20;
$thisFile=0;
while(false!=($filename=readdir($dh))){
//检查起点
如果($thisFile<$start)
继续;
$thisFile++;
//检查末端
如果($thisFile>$limit)
打破
//你的其他代码可以放在这里
}