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); } } ?> Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/ 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. Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568432 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? Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568435 Share on other sites More sharing options...
think-digitally Posted June 18, 2008 Author Share Posted June 18, 2008 anyone have any ideas? Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568450 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. Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568473 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? Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568484 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"; Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568488 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 Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568491 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. Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568493 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"; ?> Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568494 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 Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568500 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/. Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568501 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? Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568503 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. Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568507 Share on other sites More sharing options...
think-digitally Posted June 18, 2008 Author Share Posted June 18, 2008 okay, thank you Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568508 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"; Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568509 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'? Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568511 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. Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568513 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 Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568518 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... Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568939 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. Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568945 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 Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568949 Share on other sites More sharing options...
Stephen Posted June 19, 2008 Share Posted June 19, 2008 Odd, I'll have to try it. Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568951 Share on other sites More sharing options...
think-digitally Posted June 19, 2008 Author Share Posted June 19, 2008 Okay, thanks Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568952 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 :/. Link to comment https://forums.phpfreaks.com/topic/110786-solved-image-retreival/#findComment-568963 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.