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 6960607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:25:24+00:00 2026-05-27T15:25:24+00:00

i’m trying to build a little module for a site that pulls 4 random

  • 0

i’m trying to build a little module for a site that pulls 4 random photos from a folder on the server and presents them in a div. what’s happening is the same 4 photos are being called. how do i get it to call 4 different photos?

heres the random.php file:

<?php
$folder = '.';

$extList = array();
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';

$img = null;

if (substr($folder,-1) != '/') {
$folder = $folder.'/';
}

if (isset($_GET['img'])) {
$imageInfo = pathinfo($_GET['img']);
if (
    isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
    file_exists( $folder.$imageInfo['basename'] )
) {
    $img = $folder.$imageInfo['basename'];
}
} else {
$fileList = array();
$handle = opendir($folder);
while ( false !== ( $file = readdir($handle) ) ) {
    $file_info = pathinfo($file);
    if (
        isset( $extList[ strtolower( $file_info['extension'] ) ] )
    ) {
        $fileList[] = $file;
    }
}
closedir($handle);

if (count($fileList) > 0) {
    $imageNumber = time() % count($fileList);
    $img = $folder.$fileList[$imageNumber];
}
}

if ($img!=null) {
$imageInfo = pathinfo($img);
$contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
header ($contentType);
readfile($img);
} else {
if ( function_exists('imagecreate') ) {
    header ("Content-type: image/png");
    $im = @imagecreate (100, 100)
        or die ("Can't initialize image stream");
    $background_color = imagecolorallocate ($im, 255, 255, 255);
    $text_color = imagecolorallocate ($im, 0,0,0);
    imagestring ($im, 2, 5, 5,  "IMAGE ERROR", $text_color);
    imagepng ($im);
    imagedestroy($im);
}
} 

?>

here’s how random.php is being called:

<?php

/* DATA */
$data = array(
array('COUNT', '88', 'Here are a few of them', '1, 2, 3, 4'),
);
?>

<div>
<table class="reop"  border='0' width='100%'  cellpadding='0' cellspacing='10'>

<?php
$count = 0;
foreach($data as $row) {
    $class = ($count % 2 == 1 ? " class='alt'" : '');
    echo "<tr$class>";
    for($j = 0; $j < count($row); $j++) {
        if ($j!=3) {
            echo "<td class='cell_$j'>$row[$j]</td>";
        } else {


 //           $avatar = ''; 
            $array = preg_split('/,/', $row[$j], -1, PREG_SPLIT_NO_EMPTY);
            foreach ($array as $val) {
            $avatar .= '<img src="/staffpics/random.php"> ';
            }   
            echo "<td class='cell_$j'>$avatar</td>";

        }   

    }   
    echo '</tr>';
//    $count++;
}   

?>  

</table>

  • 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-05-27T15:25:25+00:00Added an answer on May 27, 2026 at 3:25 pm

    when your page is displayed, most browsers will immediately request the four pictures.. nearly all at the same time. your code for “random” picking a picture is based on time() which returns a count of seconds since the begin of unixtime. executed 4 times within the same second it will also yield the same result 4 times.

    your code:

    $imageNumber = time() % count($fileList);
    $img = $folder.$fileList[$imageNumber];
    

    should be rewritten as:

    // filelist starts with "0", count with "1" .. so we are off-by-one and first picture would never show without -1
    $imageNumber = rand() % (count($fileList) - 1);
    $img = $folder.$fileList[$imageNumber];
    

    that way you will get “random” pictures.

    Also, what Mister Lister commented is totally valid – your browser may also cache the picture and thus only request a picutre once and just display that one again and again using its cache.

    to avoid this, replace any <img src="random.php"> with <img src="random.php?rand=<?= rand(); ?>">


    But note:

    your server may eventually return the same random number multiple times so eventually (rarely) you will end up with the same picture twice on the same page.

    to fix that, you must completely rewrite your code.

    the script rendering the page should decide which pictures to choose and ensure that no picture is displayed twice instead of just including a “give random picture” script, which is independent of the page rendered.

    much easier, you could try this:

    // find all images - case SenSItivE
    $all_images = glob("/path/to/images/*.{jpeg|jpg|png|gif}", GLOB_BRACE);
    // bring array in random order
    shuffle($all_images);
    // pick four random images - you may also use a for or foreach loop to iterate the array
    list ($img1, $img2, $img3, $img4) = $all_images; 
    // write code to display four images here
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

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.