Zend - The PHP Company




Files and Directories

Add Code


Random picture/file function  

Type: code fragment
Added by: DarthZeth
Entered: 29/05/2002
Last modified: 06/12/2001
Rating: - (fewer than 3 votes)
Views: 5111
I designed this specifically as a random picture generator, but im sure it can be used for many things. You make a directory with the pictures that you want to randomly select. Using this funciton, you just use the directory as the aurgument and it returns the path of a random file in that directory.


<?PHP

/*  this will take the directory you call an dreturn a random file in it  */

function randompic ($dir)
{
/* this opens the directory and reads its contents into an array called $logo */
$handle=opendir($dir);
while (
false !== ($file readdir($handle))) { 
    if (
$file != "." && $file != "..") { 
        
$logo[] = $file;
    }
}

/* gets a random filename out of the $logo array */
$randArrayKey array_rand($logo); 
$randFile $logo[$randArrayKey];

/* puts the directory and the filename together */
$path $dir $randFile;

/* returns the full path of the file */
return($path);
}
?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories