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

  • SEARCH
  • Home
  • 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 8363203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:07:19+00:00 2026-06-09T12:07:19+00:00

I have to generate random image from a directory. I know which is simple

  • 0

I have to generate random image from a directory. I know which is simple like,

   $dire="images/";
   $images = glob($dire. '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
   $randomImage = $images[array_rand($images)];
   <input type="image" src="<?=$randomImage;?>" alt="<?=$randomImage;?>" />

But I have to make sure that each image from that directory picked at least one time before generating second time randomly. The above code only will display only any random image.

My thought is, I have to store the random image in an array and check the array every time with newly created random image. If the new random image is not in that array, I need to display that image,else I have to find another image.

I created the below code with above thought.

  $allimgs=array();
  $dire="images/";
  $images = glob($dire. '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
  $randomImage = $images[array_rand($images)];

   if(!in_array($randomImage,$allimgs))
   {
     $allimgs[]=$randomImage;
     <input type="image" src="<?=$randomImage;?>" alt="<?=$randomImage;?>" />
   }

But I am still stuck with this code. Anyone please help to improve this code? or any other idea?

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-09T12:07:21+00:00Added an answer on June 9, 2026 at 12:07 pm

    One alternative to array_rand is shuffle:

    $dire="images/";
    $images = glob($dire. '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
    shuffle($images);
    

    And then to display the next random image:

    $randomImage=array_pop($images);
    

    When the array is empty you call the initialization code again. So putting it together:

    $images=array()  //Initialize once at top of script
    $dire="images/";
    ...
    if(count($images)==0){
      $images = glob($dire. '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
      shuffle($images);
      }
    $randomImage=array_pop($images);
    

    (I deliberately repeated the glob() so that any new images get discovered on the second iteration.)

    P.S. I’m assuming you understand that, by default, PHP is stateless. If this is supposed to give each user a different image each time they visit a page (e.g. a rotating banner ad), then the code is almost the same, but move the $images array into $_SESSION.

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

Sidebar

Related Questions

I have an image gallery which pulls in random images from the folder images/flip_images/.
I have this code which is supposed to generate a captcha image of random
I want to generate random colours which can be attractive in pie charts.I have
Like my question, i need to generate random numbers that have identical pairs between
I have a project which uses php's mt_rand() to generate different random integers but
I have a project that needs to generate a random collection of images on
I have a website. On this website, I am trying to generate random images.
I have some js code which generates an image-tag like this: main_pic_div.innerHTML =<img src='+path_big_image+?+Math.random();'
I have to generate some random data, but from Regular Expression given. For example
I have the following code to generate two random numbers var attackRoll = Math.floor((Math.random()*6)+1);

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.