I would like to iterate through folders and write all the .jpg
files to a array in order to use them in the next step to create a time-lapse slideshow.
Below is the code that I already have, but how can I :
$file
to a array?jQuery
for later processing?My code so far:
<?php
$dir ="files";
$i=0;
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir)
);
$jpg_files = new RegexIterator($iterator, '/\.jpg$/'); // Dateiendung ".jpg"
foreach ($jpg_files as $file) {
if (!$file->isFile()) {
continue;
}
# echo $file->getPathname() . "
";
?>
<img src="<?php echo $file;?>"/>
<?php
}
In your loop, instead of outputting the file with <img src="<?php echo $file;?>"/>
, append it to an array:
foreach ($jpg_files as $file) {
if (!$file->isFile()) {
continue;
}
$files[] = "$file"; // appends to $files array
}
You can use json_encode
to convert the array to a format JQuery can use. There are various ways to get it into your JS environment. One way:
echo '<script> var files = ' . json_encode($files) . '</script>';