Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dir and randomly selecting help/

Status
Not open for further replies.

phphelp

Programmer
Oct 16, 2002
1
GB
I have a folder with a bunch of images in it, only gif and jpegs.

How do I get the list of images, and then randomly choose one each time the web page is refreshed?

I want to make a random image script for myself.

So maybe something like: <img src=&quot;image.php?action=image&image=/images/image.gif&quot;>

Thanks.
 
Here's an example that'll do text and images:

// $type = img or text
// $server_dir is the directory where your files are
// $ is the where your files are.

function randomizer ($type,$server_dir,$ $handle=opendir($server_dir);
$counter = 0;
//load array with file names
while ($file = readdir($handle)) {
if ($file != &quot;.&quot; && $file != &quot;..&quot;){
$arr_Images[$counter] = $file;
$counter++;
}
}
closedir($handle);
srand((double)microtime()*1000000);
$randID = rand(0,$counter - 1);
if($type==&quot;img&quot;){
echo &quot;<img src=\&quot;$www_dir/$arr_Images[$randID]\&quot; border=\&quot;0\&quot; />&quot;;
} elseif ($type==&quot;text&quot;) {
include(&quot;$server_dir/$arr_Images[$randID]&quot;);
}
} # end function


To call the function:

randomizer('[type:img|text]','[server path to directory],'[http path to directory]');

If you found this post helpful, please let me know by clicking on the appropriate link below :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top