atlanta Posted February 8, 2008 Share Posted February 8, 2008 Hi the code below wont output a random directory from the array can you check the code to see if anything is wrong thanks. <?php $cat = "/home/elvisand/public_html/photoalbum/"; function getRandomImage($path, $img, $realpath) { if ( $list = getImagesList($path) ) { mt_srand( (double)microtime() * 1000000 ); $num = array_rand($list); $img = $list[$num]; } return $realpath . $img; } function getImagesList($path) { $ctr = 0; if ( $img_dir = @opendir($path) ) { while ( false !== ($img_file = readdir($img_dir)) ) { // can add checks for other image file types here if ( preg_match("/(\.gif|\.jpg)$/", $img_file) ) { $images[$ctr] = $img_file; $ctr++; } } closedir($img_dir); return $images; } return false; } $hook = opendir($cat) or die('cant open dir'); $directories = array(); while (false!==($file = readdir($hook))) { $fpath = $cat . $file; if(is_dir($fpath)) { if($file != '.' && $file != '..') { array_push($directories, $file); } } } closedir($hook); $random = array_rand($directories, 1); echo $directories[$random[0]]; $cat2 = "/home/elvisand/public_html/photoalbum/".$directories[$random[0]]."/"; $path2 = "http://www.elvisand.com/photoalbum/".$directories[$random[0]]."/"; $default = ""; ?> <img src="<? echo getRandomImage($cat2, $default, $path2); ?>" width="158" height="168" /> Quote Link to comment Share on other sites More sharing options...
atlanta Posted February 8, 2008 Author Share Posted February 8, 2008 Found the problem $cat2 = "/home/elvisand/public_html/photoalbum/".$directories[$random[0]]."/"; $path2 = "http://www.elvisand.com/photoalbum/".$directories[$random[0]]."/"; should have been $cat2 = "/home/elvisand/public_html/photoalbum/".$directories[$random[]]."/"; $path2 = "http://www.elvisand.com/photoalbum/".$directories[$random[]]."/"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.