think-digitally Posted June 18, 2008 Share Posted June 18, 2008 Hi. I am trying to retrieve an image from a folder on the server, each time the page is refreshed a random image from the folder appears. Here is what I have got so far, and well its not working, and I am swimming far out at sea... Would be greatful to anyone that could give some help. <?php $dir = "/frontpage/"; if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { echo "<img src=".$file." />" . filetype($dir . $file) . "\n"; } closedir($dh); } } ?> Quote Link to comment Share on other sites More sharing options...
Orio Posted June 18, 2008 Share Posted June 18, 2008 You want to display a random image from that folder? <?php $dir = "/frontpage/"; $files = scandir($dir); //Note, PHP5+ $rand = rand() % (count($files) - 2) + 2; echo "<img src=".$files[$rand]." />" . filetype($dir . $file[$rand]) . "\n"; ?> Orio. Quote Link to comment Share on other sites More sharing options...
think-digitally Posted June 18, 2008 Author Share Posted June 18, 2008 Thankyou, but unfortunately I am on 4.3 Is there anyway I can do it without PHP5? Quote Link to comment Share on other sites More sharing options...
think-digitally Posted June 18, 2008 Author Share Posted June 18, 2008 anyone have any ideas? Quote Link to comment Share on other sites More sharing options...
Orio Posted June 18, 2008 Share Posted June 18, 2008 Try this: <?php function random_file ($dirname) { if(!is_dir($dirname)) return false; if(!$h = opendir($dirname)) return false; $files = array(); while($file = readdir($h) !== FALSE) if(!is_dir($file)) $files[] = $file; return $files[array_rand($files)]; } $dir = "/frontpage/"; $img = random_file($dir); if($img !== false) echo "<img src=".$img." />" . filetype($dir.$img) . "\n"; ?> Orio. Quote Link to comment Share on other sites More sharing options...
think-digitally Posted June 18, 2008 Author Share Posted June 18, 2008 Thanks again, although this time all it displays is 'dir' on screen. Any ideas? Quote Link to comment Share on other sites More sharing options...
corbin Posted June 18, 2008 Share Posted June 18, 2008 You could just emulate the scandir function in his code from earlier... $dir = "/frontpage/"; if(!function_exists('scandir')) { function scandir($dir) { $d = opendir($dir); $r = array(); while(false !== ($f = readdir($d))) { $r[] = $f; } return $r; } } $files = scandir($dir); $rand = rand() % (count($files) - 2) + 2; echo "<img src=".$files[$rand]." />" . filetype($dir . $file[$rand]) . "\n"; Quote Link to comment Share on other sites More sharing options...
think-digitally Posted June 18, 2008 Author Share Posted June 18, 2008 I tried this one, sorry to say it doesnt work, the entire page goes blanks Quote Link to comment Share on other sites More sharing options...
corbin Posted June 18, 2008 Share Posted June 18, 2008 Ahhh I had a typing error.... My bad.... I'll edit my other post to be correct. Quote Link to comment Share on other sites More sharing options...
corbin Posted June 18, 2008 Share Posted June 18, 2008 Well crap.... Editing the old post is breaking the HTML.... I guess I'll repost it... Sorry for the multiple posting x.x. <?php $dir = "/frontpage/"; if(!function_exists('scandir')) { function scandir($dir) { $d = opendir($dir); $r = array(); while(false !== ($f = readdir($d))) { $r[] = $f; } return $r; } } $files = scandir($dir); $rand = rand() % (count($files) - 2) + 2; echo "<img src=".$files[$rand]." />" . filetype($dir . $file[$rand]) . "\n"; ?> Quote Link to comment Share on other sites More sharing options...
think-digitally Posted June 18, 2008 Author Share Posted June 18, 2008 No problem, thanks for helping I am afraid there are errors Warning: opendir(/2/): failed to open dir: No such file or directory in /home/content/a/1/6/a16538296/html/index1.php on line 12 Warning: readdir(): supplied argument is not a valid Directory resource in /home/content/a/1/6/a16538296/html/index1.php on line 14 Warning: filetype(): Lstat failed for (null) (errno=2 - No such file or directory) in /home/content/a/1/6/a16538296/html/index1.php on line 23 I have checked the name of the folder, and its defo correct, so not sure Quote Link to comment Share on other sites More sharing options...
corbin Posted June 18, 2008 Share Posted June 18, 2008 If it were correct, you wouldn't be getting errors ;p.... Well, you might still.... Does PHP have read access to the target directory? Also, /2/ means the directory 2 from the root of the drive.... Do you mean the root of the drive, or do you want PHP to access a directory a level below the php file? If so, it needs to be 2/, not /2/. Quote Link to comment Share on other sites More sharing options...
think-digitally Posted June 18, 2008 Author Share Posted June 18, 2008 ha ha I guess so Well I changed to 2/ and the errors have gone... but it now just displays 'dir' Any ideas? Quote Link to comment Share on other sites More sharing options...
corbin Posted June 18, 2008 Share Posted June 18, 2008 Uhhh, sec lemme test it on my comp. Quote Link to comment Share on other sites More sharing options...
think-digitally Posted June 18, 2008 Author Share Posted June 18, 2008 okay, thank you Quote Link to comment Share on other sites More sharing options...
corbin Posted June 18, 2008 Share Posted June 18, 2008 Found the error.... echo "<img src=".$files[$rand]." />" . filetype($dir . $file[$rand]) . "\n"; Should be echo "<img src=".$files[$rand]." />" . filetype($dir . $files[$rand]) . "\n"; Quote Link to comment Share on other sites More sharing options...
think-digitally Posted June 18, 2008 Author Share Posted June 18, 2008 great, although it now says 'file; instead of 'dir'? Quote Link to comment Share on other sites More sharing options...
corbin Posted June 18, 2008 Share Posted June 18, 2008 That's because of the filetype() call.... I'm not quite sure why Orio put that there... If you don't want it there, just remove the filetype() call. Quote Link to comment Share on other sites More sharing options...
think-digitally Posted June 18, 2008 Author Share Posted June 18, 2008 okay sorry to be a pain.. I remove it and it show nothing at all :S heres what I changed... echo "<img src=".$files[$rand]." />" . "\n"; I want it to display the images from the folder randomly each time the page is refreshed Quote Link to comment Share on other sites More sharing options...
think-digitally Posted June 19, 2008 Author Share Posted June 19, 2008 Anyone have any ideas? So close... Quote Link to comment Share on other sites More sharing options...
Stephen Posted June 19, 2008 Share Posted June 19, 2008 Couldn't you do like: <?php $top=10; //How many images you have... or anything I guess. $dir="/dir/"; $od=opendir($dir); $rand=rand(1,$top); $done=false; while (!$done) { while ($file=readdir($od)) { $nr=rand(1,$top); if ($nr==$rand) { $done=true; header("Content-type image/".filetype($dir."".$file)); readfile($dir."".$file); } } } closedir($od); ?> Not tested . I hope it works, I'm kind of tired right now though. Quote Link to comment Share on other sites More sharing options...
think-digitally Posted June 19, 2008 Author Share Posted June 19, 2008 Hmm, thankyou, but it doesnt seem to work, it shows an internal server error Quote Link to comment Share on other sites More sharing options...
Stephen Posted June 19, 2008 Share Posted June 19, 2008 Odd, I'll have to try it. Quote Link to comment Share on other sites More sharing options...
think-digitally Posted June 19, 2008 Author Share Posted June 19, 2008 Okay, thanks Quote Link to comment Share on other sites More sharing options...
Stephen Posted June 19, 2008 Share Posted June 19, 2008 Well <?php $top=6; //How many images you have... or anything I guess. $dir="/dir/"; $od=opendir($dir); $rand=rand(1,$top); $ndone=true; while ($ndone) { while ($file=readdir($od)) { $nr=rand(1,$top); if ($nr==$rand) { readfile($dir."".$file); $ndone=false; } } } closedir($od); ?> Grabs a random file and reads the contents. Problem is, I can't get the header to work (above readfile) so it can display as an image :/. 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.