dreamwest Posted July 12, 2009 Share Posted July 12, 2009 is it possible to scandir a random file?? This code selects the first file found $dir = '/htdocs/user/site.com/thumbs/'; foreach(scandir($dir) as $file){ if (strtolower(substr($file, -4)) == '.jpg') { if (file_exists($dir . $file)) { $image = "/thumbs/".$file; echo $image; } else { //default image echo "There was an error getting the thumbnail "; } } }//end foreach Link to comment https://forums.phpfreaks.com/topic/165681-solved-scandir/ Share on other sites More sharing options...
ignace Posted July 12, 2009 Share Posted July 12, 2009 $scandir = scandir('/path/to/directory'); do { $file = array_rand($scandir); } while (!is_file($file)); $dir . $file is unneccessary as $file already has the directory prepended. Link to comment https://forums.phpfreaks.com/topic/165681-solved-scandir/#findComment-873949 Share on other sites More sharing options...
dreamwest Posted July 12, 2009 Author Share Posted July 12, 2009 Excellent Thanks! Link to comment https://forums.phpfreaks.com/topic/165681-solved-scandir/#findComment-873953 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.