I want to show the first 10 files, but not scan the entire directory. Show the first 10, then another 10 and so on until the end.
I found this script, but it doesn’t actually work.
<?php
$start = 3;
$limit = 5;
$thisFile = $start;
$files = array();
if($_GET['do'] === "articles") {
if($handle = opendir('./articles/')) {
while (false !== ($entry = readdir($handle))) {
if($entry == "." || $entry == "..") {
continue;
}
// Check the start
if($thisFile < $start) {
continue;
}
$thisFile++;
// Check the end
if($thisFile > $limit) {
break;
}
$files[$thisFile] = $entry;
print_r($files);
}
closedir($handle);
}
}
?>
I have 9 txt files in directory and if i use this code, its give me this:
Array ( [4] => 1.txt ) Array ( [4] => 1.txt [5] => 2.txt )
Why script gives me first files, not from 3 to 5?
New contributor
c4ika is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.