   [web] PHP Random Screenshot Code
  I'm trying to get the contents of a directory and then put all the file names with jpg extension to an array.Then I try to pick a random filename from the array and disply it.However the array is empty after the while loop and I can't get anything to display.If I echo from the array inside the while loop I get the file names [oh]<? $arrayNames = array();$fileCount=0;$filePath=$PATH.'darkage/screenshots/';$dir = opendir($filePath); # Open the pathwhile ($file = readdir($dir)) {   if (eregi("\.jpg",$file)) {$arrayNames[fileCount] = $file;    $fileCount++;  }}$random = rand(0,$fileCount);$imgSrc=$arrayNames[$random];echo $arrayNames[0]; //doesn't display anythingecho $imgSrc; //doesn't display anything?><a href="darkage/screenshots/<? echo $imgSrc; ?>"><img width=150 src="darkage/screenshots/<? echo $imgSrc;?>"></a>
  You have "$arrayNames[fileCount]" instead of "$arrayNames[$fileCount]" inside the inner loop.   ;  Gah!!I knew it was something like that,it is always the $$$ [grin]Thanks mate It works now.   ;Quote:Original post by Black KnightA shortened version of your code:<?php$globbed = $PATH.'darkage/screenshorts/*.jpg';$files   = glob($globbed);$display = $files[rand(0,count($files))];$uri= '/darkage/screenshots'.basename($display);?><a href="<?=$uri?>"><img width=150 src="<?=$uri?>"/></a>;  Wow,didn't know there was a handy function like glob().Thanks for the info.I just added "/" at the end, or it becomes /darkage/screenshotsgame011.jpg etc.<?$globbed = $PATH.'darkage/screenshots/*.jpg';$files   = glob($globbed);$display = $files[rand(0,count($files))];$uri= '/darkage/screenshots/'.basename($display);?><a href="<?=$uri?>"><img width=150 src="<?=$uri?>"/></a>;  Note that there is a small but significant difference between glob and readdir in this case. glob is case sensitive. That means it won't find your files if the extension is ".JPG" rather than ".jpg". Your other code takes that into account by using eregi. It might not be an issue, but if it is, change the glob lines to:$globbed = $PATH.'darkage/screenshots/*.{J,j}{P,p}{G,g}';$files   = glob($globbed, GLOB_BRACE);;  Also, instead of this:$display = $files[rand(0,count($files))];you can use:$display = $files[array_rand($files)];[Edited by - Sander on May 19, 2009 3:13:52 PM];Quote:Original post by Sander$display = array_rand($files);If I remember correctly, that should be $files[array_rand($files)].;  You're right. My bad. I fixed my post above.   