note 28021 deleted from function.strstr by felipe

From: Date: Wed, 16 Apr 2008 11:10:39 +0000
Subject: note 28021 deleted from function.strstr by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-138363@lists.php.net to get a copy of this message
Note Submitter: mario at comicide dot com ---- 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\">";

« previous php.notes (#138363) next »