1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to get one random image from ten thousand folders using glob?

Discussion in 'PHP' started by ipunkbali, Mar 11, 2016.

  1. #1
    I have thousand directory folders like this:

    img/monkey-folder/ cute-monkey.jpg etc. etc
    img/turkey-folder/
    img/rose-folder/
    img/big-fish-folder/
    img/more-folder/
    etc.

    Each folder has 20 images.

    How to get only 1 random image?

    This code below is working but only pull 1 image from 1 folder. I need one random image from multiple folders. thanks

    <?php
    $imagesDir = 'img/monkey-folder/';
    $images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
    $randomImage = $images[array_rand($images)];
    echo $randomImage; //output img/monkey-folder/etc.jpg
    ?>
    Code (markup):

     
    Solved! View solution.
    ipunkbali, Mar 11, 2016 IP
  2. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #2
    I think this could do that for you

    
    <?php
    $imagesDir = glob('img/*', GLOB_ONLYDIR);
    $randomfolder = $imagesDir[array_rand($imagesDir)];
    $images = glob($randomfolder . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
    $randomImage = $images[array_rand($images)];
    echo $randomImage; //output img/monkey-folder/etc.jpg
    ?>
    
    Code (markup):
     
    KangBroke, Mar 11, 2016 IP
    sarahk likes this.
  3. ipunkbali

    ipunkbali Well-Known Member

    Messages:
    276
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #3
    Thanks but still not working :( . Is it better to use glob or readdir? I want to display one image from many folders.
     
    ipunkbali, Mar 12, 2016 IP
  4. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #4
    <?php
    $imagesDir = glob('img/*', GLOB_ONLYDIR);
    $randomfolder = $imagesDir[array_rand($imagesDir)];
    $images = glob($randomfolder . '/*.{jpg,jpeg,png,gif}', GLOB_BRACE);
    $randomImage = $images[array_rand($images)];
    echo $randomImage; //output img/monkey-folder/etc.jpg
    ?>
    Code (markup):
    try this, may be the missing / on line 4
     
    KangBroke, Mar 12, 2016 IP
  5. #5
    This code does work, I have tested it.
    It was missing the slash.
    One thing I did notice, I have some images as .JPG not .jpg, and .PNG not .png. And so on.

    So you really may want to use this code instead.

    <style>
    .image {
        width:100px;
        height:100px;
    }
    </style>
    <?php
    $imagesDir = glob('Car/*', GLOB_ONLYDIR);
    $randomfolder = $imagesDir[array_rand($imagesDir)];
    $images = glob($randomfolder . '/*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}', GLOB_BRACE);
    $randomImage = $images[array_rand($images)];
    echo '<img src="'.$randomImage.'" class="image">'; 
    ?>
    Code (markup):
     
    KangBroke, Mar 12, 2016 IP
  6. ipunkbali

    ipunkbali Well-Known Member

    Messages:
    276
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #6
    Thank you. It workssssssss!! :)
     
    ipunkbali, Mar 12, 2016 IP