note 90770 modified in function.readdir by danbrown
| From: | danbrown@php.net | Date: | Fri, 19 Jun 2009 19:09:23 +0000 |
| Subject: | note 90770 modified in function.readdir by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-157135@lists.php.net to get a copy of this message | ||
Thought I would include what I wrote to get a random image from a directory.
<?php
$image_dir = 'images';
$count = 0;
if ($handle = opendir($image_dir)) {
$retval = array();
while (false !== ($file = readdir($handle))) {
if (($file <> ".") && ($file <> "..")) {
$retval[$count] = $file;
$count = $count + 1;
}
}
closedir($handle);
}
shuffle($retval);
$current_image = $retval[0];
?>
[NOTE BY danbrown AT php DOT net: Contains a bugfix/typofix inspired by 'ffd8' on
19-JUN-09.]
--was--
Thought I would include what I wrote to get a random image from a directory.
<?php
$count = 0;
if ($handle = opendir('image_dir)) {
$retval = array();
while (false !== ($file = readdir($handle))) {
if (($file <> ".") && ($file <> "..")) {
$retval[$count] = $file;
$count = $count + 1;
}
}
closedir($handle);
}
shuffle($retval);
$current_image = $retval[0];
?>
http://php.net/manual/en/function.readdir.php