• Resolved kasperbs

    (@kasperbs)


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

    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 images to display independently.

    Thanks in advance

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

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

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

    Resource:
    https://us3.php.net/glob
    https://us3.php.net

    Thread Starter kasperbs

    (@kasperbs)

    Thanks MichaelH, I’m looking into it now.

    Thread Starter kasperbs

    (@kasperbs)

    Thanks 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 asked in my original question. So if anyone is interested 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 image.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘PHP Array Question’ is closed to new replies.