Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9127331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:15:31+00:00 2026-06-17T07:15:31+00:00

I have a php code which uses every image in a folder and echos

  • 0

I have a php code which uses every image in a folder and echos the url on my page.

what i need help with is making the php code randomize the list of urls each time the page is loaded.

The code that i have is:

<?php 
 if ($handle = opendir('images')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != "..")
      {
            $thelist .= '<div data-delay="5"><img src="images/'.$file.'"></div>';
          }
       }
  closedir($handle);
  }
?>
<?=$thelist?>

many thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:15:32+00:00Added an answer on June 17, 2026 at 7:15 am

    Easiest solution would be to put all filenames into an array and then use shuffle() to mix it up. Then you can iterate over the array and output the images. It should look something like this:

    <?php 
     $thelist = "";
     if ($handle = opendir('images')) {
       $images = array();
       while (false !== ($file = readdir($handle))) {
          if ($file != "." && $file != "..") {
                array_push($images, 'images/'.$file);
          }
       }
       closedir($handle);
       shuffle($images);
       foreach ($images as $image) {
          $thelist .= '<div data-delay="5"><img src="'.$image.'"></div>';
       }
       echo $thelist;
     }
    ?>
    

    By using glob() instead of opendir() you could shorten the code significantly, as glob() returns an array and then you only would need to shuffle that one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the php code below which help me get a photo's thumbnail image
I'm having a strange problem. I have a HTML page with PHP code which
I have this code, which uses ob_start php function. Which basically puts the echoed
Under plain PHP 5.3, I have some code which uses MySQL to first deletes
I have PHP code which connects to MySql and encodes data to JSON. Later
I have written PHP code which connects to MYSQL to look up information, sends
I have some PHP code which allows me to sort a column into ascending
I have some PHP code which stores whatever is typed in a textbox in
I have the following php code which I want to add a delay too:
I have the following PHP code which works out the possible combinations from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.