While Loop Problem

Discussion in 'PHP' started by . Jordan ., Jul 25, 2010.

  1. #1
    I'm trying to select a random image from a folder. However, since scandir() returns two 'files' named '.' and '..' (which aren't even in the folder lol) I wrapped the array_rand() function in a while loop. Unfortunately this causes the echo() below it to not execute. Any ideas why this is happening. Oh and btw, I'm using the echo() so I can check the value of the $filename variable.

    <?php
    
    $dir = 'imgs';
    
    $imgs = scandir($dir);
    
    while($filename == '.' || $filename == '..') {
    
    $rand = array_rand($imgs, 1);
    
    $filename = $imgs[$rand];
    
    }
    
    echo('<br /><br />' . $filename);
    
    ?>
    PHP:

     
    . Jordan ., Jul 25, 2010 IP
  2. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <?php      
        $dir = 'imgs'; 
        $imgs = scandir($dir);
    
        foreach ($imgs as $image) {
            // . is the current directory, .. is the parent directory
            if ($image == '.' || $image == '..') continue;
            
            $images[] = $image;
        }
    
        shuffle($images); 
    
        // random image
        echo $images[0];
    ?>
    
    PHP:
     
    Deacalion, Jul 25, 2010 IP
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    <?php
    
    $dir = 'imgs';
    
    $imgs = scandir($dir);
    
    foreach($imgs as $img) {
    if ($img == '.' || $img == '..') {
    unset($imgs[array_search($img, $imgs)]);
    }
    }
    
    $rand = array_rand($imgs, 1);
    
    $filename = $imgs[$rand];
    
    echo $filename;
    
    ?> 
    PHP:
     
    danx10, Jul 25, 2010 IP
  4. Nick66

    Nick66 Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    <?php
    function getImgList($dir) {
    //array to hold return value 
    $retval = array();
    // add trailing slash if missing 
    if(substr($dir, -1) != "/") $dir .= "/"; 
    // open pointer to directory and read list of files 
    $d = @dir($dir) or die("getImgList: Failed opening directory $dir for reading"); 
    while(false !== ($entry = $d->read())) {
    // skip hidden files 
    if($entry[0] == ".") continue;
    // skip directories
    if(is_dir("$dir$entry")) continue;
    if(is_readable("$dir$entry")) {
    //check if file type is an image
    if(substr(mime_content_type("$dir$entry"),0,5) == 'image') {
    $retval[] = $dir.$entry;
    }
    } 
    } 
    $d->close(); 
    return $retval; 
    }
    
    /*
    You can use this function as follows:
    $dirlist = getImgList(".");  Use this if you put your code in the same directory where the images are
    $dirlist = getImgList("./"); 
    $dirlist = getImgList("images"); 
    $dirlist = getImgList("images/"); Use this if you put your code outside of the directory where the images are
    $dirlist = getImgList("./images"); 
    $dirlist = getImgList("./images/"); ...
    */
    
    $dirlist = getImgList("imgs/"); //generate array with picts in directory
    $numofimages = count($dirlist); //number of images in directory
    $pl = 1; // here you can set how many random images you want to pick
    if($pl>$numofimages)$pl=$numofimages; // check not to pick more random picts than available
    unset($rndimg);
    $rndimg = array_rand($dirlist, $pl); //generate array with random picts
    //print random images
    if($pl>1) {
    for ($i = 0; $i <$pl; $i++) {
    echo $dirlist[$rndimg[$i]];
    echo "<br />";
    }
    } else {
    echo $dirlist[$rndimg];
    }
    ?>
    PHP:
    You can select as many random pictures as you want, by setting the $pl value.
    You can select a directory including any type of files along with images.
    Sub directories ignored.
     
    Nick66, Jul 26, 2010 IP