Read all sub directories within a certain folder to display a random image
<p>I have this code i have been using....but i need a conditional where it will read all the sub directories of /bg to select an image as opposed to a specific folder if they were on a subpage.</p>

<p>Heres my code so far which works perfectly for all subpages to display specific images:</p>

<pre><code>//This would tell us its on the homepage if it helps:
$this-&gt;level() == 0

//This is the code so far
$path = '/home/sites/mydomain.co.uk/public_html/public/images/bg/'.$this-&gt;slug;


$homepagefile = URL_PUBLIC.'public/images/bg/'.$this-&gt;slug.'/main.jpg';

$bgimagearray = array();
$iterator = new DirectoryIterator($path);
foreach ($iterator as $fileinfo) {
    if ($fileinfo-&gt;isFile() &amp;&amp; !preg_match('\.jpg$/', $fileinfo-&gt;getFilename())) {
        $bgimagearray[] = "'" . $fileinfo-&gt;getFilename() . "'";
    }
}

$bgimage = array_rand($bgimagearray);

?&gt;

&lt;div id="bg"&gt;
    &lt;div&gt;
        &lt;table cellspacing="0" cellpadding="0"&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;img src="&lt;?php echo $file.trim($bgimagearray[$bgimage], "'"); ?&gt;" alt=""/&gt;&lt;/td&gt;
            &lt;/tr&gt;
        &lt;/table&gt;
    &lt;/div&gt;
&lt;/div&gt;
</code></pre>

<p>Any help would be appreciated, im sure its not rocket science but ive tried a few ways and cant get my head around it.</p>

<p>Thanks in advance. </p>
