note 28021 added to function.strstr
| From: | mario at comicide dot com | Date: | Mon, 30 Dec 2002 04:58:52 +0000 |
| Subject: | note 28021 added to function.strstr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-41451@lists.php.net to get a copy of this message | ||
If you would like to count files on a directory, this might be helpful. In this case I wanted to
create a random image generator, but not have to keep track of the image count. Example: I have 4
images, and choose a random number between 1-4. If I decide to add a 5th and 6th image, I would
have to generate at random 1-6. STRSTR can help you keep track of the amount of images, without you
having to update the code.
NOTE, this example is based on the naming convention
image1.gif
image2.gif
image3.gif
""""""""4"""
etc....
If you are not using it, then just make the adjustments.
------------------------------------------------------
//First a function. $path is where you want to count files
//$filter is the criteria. Ex. File names with "image" in it.
function countfiles($path, $filter)
{
$dir = opendir($path);
while ($file = readdir($dir)){
if(strstr($file, $filter)){ $i++; }
}//end while
closedir($dir);
return $i;
}
//-------------------------------------------------
$max = countfiles("/you directory", "image");
$num = rand(1,$max);
$image = "image";
$image.= $num;
$image.= ".gif"; // or jpg, png, etc
echo "<img scr=\"$image\">";
--
http://www.php.net/manual/en/function.strstr.php
http://master.php.net/manage/user-notes.php?action=edit+28021
http://master.php.net/manage/user-notes.php?action=delete+28021
http://master.php.net/manage/user-notes.php?action=reject+28021