|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
show a random image
How do I select an image at random from a certain dir. ??
Marc |
|
#2
|
||||
|
||||
|
hi,
i m not giving u exact code. but i can give u idea. read all the images from directory in to array. i mean image names in array. i hope how to do that. after that find how many elements are tehre in array. that reflects no. of images in array. now keep that as max. no. in generating random no.and use the generated random no. as a key to array get the filename and display it. i hope u get the idea, if u cant get idea, or cant write code, post back, jd
__________________
_____________________________ d.k.jariwala (JD) ~ simple thought, simple act ~ I blog @ http://jdk.phpkid.org |
|
#3
|
|||
|
|||
|
Code:
$dir = '/home/path/to/randomimage';
# This is the web path to directory that holds the images
$weburl = 'http://www.yoursite.com/randomimage';
########################################################
# The script
########################################################
opendir(DIR, $dir) || die "$!";
while ($_ = readdir DIR)
{
if(-f "$dir/$_"){
push(@dir, $_);
}
}
closedir(DIR);
$count = scalar @dir;
$i = int(rand( $count ));
print "Content-type: text/html\n\n";
print "<img src=\"$weburl/$dir[$i]\">";
This is just copied from a script i wrote a really long time ago that does exactly what you want.
__________________
Jon Coulter ledjon@ledjon.com |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > show a random image |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|