My goal is, to create a web based gallery.
To achieve this, i'm reading out a directory, store the file-names within an array and returning the array back, to load the images. This works.
But, the images are not sorted, or, to be precise, the images are sorted in a non intuitive way. This way works:
$allFiles = array();
$count = 0;
if (is_dir($thumbnail_dir)) {
if ($dh = opendir($thumbnail_dir)) {
// Read files
while (($file = readdir($dh)) !== false) {
if($file != '' && $file != '.' && $file != '..') {
$allFiles[$count] = $file;
$count ;
}
}
$count--;
}
}
echo json_encode($allFiles);
If i return this to my JavaScript, it works, but the images are not really sorted. To get the the images sorted, in a way, I define, I add this, and it works too.
sort($allFiles);
echo json_encode($allFiles);
But, beacuse I have to many images, to load all at once, I added a loop, to select a chunk of file an return only this chunk. I want to load initially 20 images on my page, so the first call will be $firstIndex = 0 and $lastIndex = 20; the second call will be $firstIndex = 20 and $lastIndex = 40; and so on. Without chunking, it works perfect. But with chunking, it chrashs.
for ($i = $firstIndex; i < $lastIndex; $i ) {
$j = $i - $firstIndex;
$fileNames[$j] = $allFiles[$i];
}
echo json_encode($fileNames);
At this point must be occur an error and nothing appears on my page.
What do I wrong?
Greets Johannes
CodePudding user response:
I think you missed a $
sign for i
at i < $lastIndex
for ($i = $firstIndex; $i < $lastIndex; $i ) {
$j = $i - $firstIndex;
$fileNames[$j] = $allFiles[$i];
}
echo json_encode($fileNames);