Question

<?php

$imgdir = "";

//Change time to how often the images should change (in seconds)
$time = 10;

header('content-type: image/jpg');
if($imgdir != "") {
    $files = glob("/" . $imgdir . "/*/*.jpg");
} else {
    $files = glob("*.jpg");
}
$imagename = $files[abs((time() / $time) % (count($files)))];
echo file_get_contents($imagename);

This script loops through a directory of jpgs in order. I want to make it so that it will loop through a directory and its sub directories in order. I've tried using an asterisk where the subfolder name should be in $files but that did not work. I'm unsure as to how I could accomplish this. Any insight would be greatly appreciated.

Was it helpful?

Solution

Try using this recursive method I found on glob() documentation (by peter dot adrianov at gmail dot com).

function scandir_through($dir) {
    $items = glob($dir . '/*.jpg');

    for ($i = 0; $i < count($items); $i++) {
        if (is_dir($items[$i])) {
            $add = glob($items[$i] . '/*');
            $items = array_merge($items, $add);
        }
    }

    return $items;
}

$files = scandir_through($imgdir);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top