开发者

Creating image list from random folder

I have this PHP code below that i would like to adapt a little with some help.

I need to produce a list of images based on the URL pointing to a specific folder (using expression engine).

Currently this code works really well but i need it to do two more things...

  1. To move up a folder if it doesn't find one... e.g if it doesn't find directory/subdirectory it will go off and search in directory instead.(this would need to stop obviously at a certain level)

  2. Produce a list of all images in the folder, not just one like below.

    <?php
    
    $bgimagearray = array();
    $iterator = new DirectoryIterator("sites/domain.co.uk/public_html/assets/images/bg-images/{last_segment}");
    foreach ($iterator as $fileinfo) {
        if ($fileinfo->isFile() &am开发者_如何学运维p;& !preg_match('/-c\.jpg$/', $fileinfo->getFilename())) {
            $bgimagearray[] = "'" . $fileinfo->getFilename() . "'";
        }
    }
    
    $bgimage = array_rand($bgimagearray);
    ?>
    
    
    <div id="backgroundImage"> 
            <img src="{site_url}assets/images/bg-images/{last_segment}/<?php echo trim($bgimagearray[$bgimage], "'"); ?>" alt="{last_segment}" />
    </div>
    


On the first issue, you can use dirname php function. For the other issue, the one of getting all the files instead of one, you already have the whole array in $bgimagearray, so you can iterate it with a foreach.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜