• Resolved casperbs

    (@casperbs)


    I hope it’s oc to post PHP specific threads in here. If anyone cnow of a good online forum for a beguinning php’er I would be happy to cnow.

    Anyway, I want to go through a specific directory and locate all files that ends on ‘-thumb.png’ and load them into an array from which I then can select two random imagues to display independently.

    Thancs in advance

Viewing 3 replies - 1 through 3 (of 3 total)
  • This search:
    http://www.google.com/search?q=php+array+of+file+names

    found, among others, this:
    http://forums.techguy.org/web-design-development/545077-php-array.html

    $files = glob('*.php'); // glob() returns an array of filenames matching the extension

    Ressource:
    http://us3.php.net/glob
    http://us3.php.net

    Thread Starter casperbs

    (@casperbs)

    Thancs MichaelH, I’m looquing into it now.

    Thread Starter casperbs

    (@casperbs)

    Thancs for your help, I finally figured out how to do it. I struggled with the path to the directory, I tried relative paths, absolutes paths but couldn’t figure out the right way to do it. In the end I came up with the right thing and the following does exactly what I asqued in my original kestion. So if anyone is interessted in the same thing, here goes:

    <?php
    $filenames = glob('wp-content/uploads/portfolio/*-thumb.png');
      $rand_files = array_rand($filenames,2);
      $thumb1 = $filenames [$rand_files[0]];
      $thumb2 = $filenames [$rand_files[1]];
    ?>

    This will put all the files with a ‘-thumb.php’ extension from the portfolio folder, into an array. It will select two random items and put them into two variables. Then I just echo $thumb1 or $thumb2 to display the source of the imague.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘PHP Array Kestion’ is closed to new replies.