473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Choose random image from dir and display it

I have a directory of images, called "random". In it are the following
files:

1.gif
2.gif
3.gif
4.gif

I use this script to choose a random image and display it:

$i = -2; // $i = -2 so that the directory files ("." and "..") won't be
counted.
if ( $dh = opendir( "images/random" ) ) {
while ( false !== ( $file = readdir( $dh ) ) ) {
$i++;
}
closedir( $dh );
$rand = rand( 1, $i );
$randimg = '<img src="images/random/' . $rand . '.gif" />';
}
print $randimg;

This works just dandy. However, I figure there must be a more efficient way
to count the number of files in a directory. Is there a directory's
equivalent of a function like file()? Or is my script the only way to count
the files?

Thanks.
- JP
Jul 17 '05 #1
4 3398
You probably find it safer to keep doing it the way you are.

Or instead modify it to build an array containing filenames and then
randomly pick an entry from the array. This is to account for if a file
is ever removed, no errors would occur.
kingofkolt wrote:
I have a directory of images, called "random". In it are the following
files:

1.gif
2.gif
3.gif
4.gif

I use this script to choose a random image and display it:

$i = -2; // $i = -2 so that the directory files ("." and "..") won't be
counted.
if ( $dh = opendir( "images/random" ) ) {
while ( false !== ( $file = readdir( $dh ) ) ) {
$i++;
}
closedir( $dh );
$rand = rand( 1, $i );
$randimg = '<img src="images/random/' . $rand . '.gif" />';
}
print $randimg;

This works just dandy. However, I figure there must be a more efficient way
to count the number of files in a directory. Is there a directory's
equivalent of a function like file()? Or is my script the only way to count
the files?

Thanks.
- JP

Jul 17 '05 #2
neur0maniak <us****@neur0maniak.co.uk> wrote in news:41830267$0$43631
$e*******@ptn-nntp-reader04.plus.net:
You probably find it safer to keep doing it the way you are.

Or instead modify it to build an array containing filenames and then
randomly pick an entry from the array. This is to account for if a file
is ever removed, no errors would occur.


Try this, as described above. Forget where I found the code but it works
just fine. $dirpath should be a fullpath to the appropriate directory and
requires a slash at the end... or you can add it to the couple of lines
in between the path and the image filename. Everything in the directory
except those in the if statement will be put into the array.

$dir_contents = array();
$d = dir($dirpath);
while ( $crnt_file = $d->read() ) {
if ($crnt_file == "." || $crnt_file == ".."
|| is_dir($dirpath.crnt_file))
continue;
$dir_contents[] = $dirpath.$crnt_file;
}
$d->close();
Jul 17 '05 #3
Fox
kingofkolt wrote:
I have a directory of images, called "random". In it are the following
files:

1.gif
2.gif
3.gif
4.gif

I use this script to choose a random image and display it:

$i = -2; // $i = -2 so that the directory files ("." and "..") won't be
counted.
if ( $dh = opendir( "images/random" ) ) {
while ( false !== ( $file = readdir( $dh ) ) ) {
$i++;
}
closedir( $dh );
$rand = rand( 1, $i );
$randimg = '<img src="images/random/' . $rand . '.gif" />';
}
print $randimg;

This works just dandy. However, I figure there must be a more efficient way
to count the number of files in a directory. Is there a directory's
equivalent of a function like file()? Or is my script the only way to count
the files?

Thanks.
- JP


php 4.3 or greater:

$imgGroup = glob("images/random/{*.jpg,*.gif,*.png}", GLOB_BRACE);

echo "
<img src = {$imgGroup[rand(0, count($imgGroup)-1)]} />
";

Fox
**************

Jul 17 '05 #4
"kingofkolt" <je**********@comcast.net> wrote in message news:<WMBgd.340547$D%.310394@attbi_s51>...
I have a directory of images, called "random". In it are the following
files:

1.gif
2.gif
3.gif
4.gif

I use this script to choose a random image and display it:

$i = -2; // $i = -2 so that the directory files ("." and "..") won't be
counted.
if ( $dh = opendir( "images/random" ) ) {
while ( false !== ( $file = readdir( $dh ) ) ) {
$i++;
}
closedir( $dh );
$rand = rand( 1, $i );
$randimg = '<img src="images/random/' . $rand . '.gif" />';
}
print $randimg;

This works just dandy. However, I figure there must be a more efficient way
to count the number of files in a directory. Is there a directory's
equivalent of a function like file()? Or is my script the only way to count
the files?

Thanks.
- JP


Yes. You can do all that in a couple lines:

$file = array_rand(glob("images/random/*.gif"));
echo "<img src=\"$file\">";
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: lucy | last post by:
Hello comp.lang.js.I need a script to rotate a background image on each newpage load, given the id of the element and a list (array?)of 2 or more image paths ("img1.jpg", "img2.jpg", ...).Actually,...
4
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
7
by: Spartanicus | last post by:
Afaik the use of a <noscriptelement is frowned upon nowadays. For a JS random image changer I tried to use a replacement by having the script change the HTML src attribute value of an img element....
4
by: Kim | last post by:
Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the...
0
by: Kim | last post by:
Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the...
1
by: KoRnDragon | last post by:
I need a Javascript function that will display X random images on a page that also doesn't display the same image twice. Bonus points if it grabs the max array from the folder and sets it. Even...
1
by: Sahus Pilwal | last post by:
Hi, I hope someone can help me with this. I'm new to .NET and in fact server side programming and have a small query I'm sure... I'm using the System.IO Namespace with a For - each and If then...
8
by: Kentor | last post by:
Hello, I have users that submit images to my website. By default I choose to make the width bigger than the height because usually pictures are taken horizontally... But in some cases people take...
10
by: lifity | last post by:
hi, i am stuck on part of my project and very new to VB. i had cut my picture into 6 equals part but need to display them randomly, without repeating the same part of the pic again, in random...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.