Stuve Posted August 15, 2006 Share Posted August 15, 2006 Hey!I want to show a random picture on my start page. The picture is in sub folders.I have used this code to list the files but can somebody help me so only one randomed file is selected and so it only selects picture files from a selected sub folder.[code]<?php$rep=opendir('.');while ($file = readdir($rep)) { if ($file != '..' && $file !='.' && $file !='' && $file !=is_dir($file)) { echo "<img src=\"$file\">"; }}closedir($rep);clearstatcache();?>[/code]Thanx!! Quote Link to comment https://forums.phpfreaks.com/topic/17633-random-file-select/ Share on other sites More sharing options...
Daniel0 Posted August 15, 2006 Share Posted August 15, 2006 A modification of your code:[code]<?php$rep=opendir('.');while ($file = readdir($rep)) { if ($file != '..' && $file !='.' && $file !='' && $file !=is_dir($file)) { $images[] = $file; }}closedir($rep);clearstatcache();$random_file = $images[rand(0,count($images)-1)];echo "<img src='{$random_file}' alt='random image' />";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17633-random-file-select/#findComment-75153 Share on other sites More sharing options...
redarrow Posted August 15, 2006 Share Posted August 15, 2006 Baniel can you kindly exsplin this code please as simple as posable cheers.I am trying to understand it for future refrence cheers mate.sorry to but in[rand(0,count($images)-1)]; Quote Link to comment https://forums.phpfreaks.com/topic/17633-random-file-select/#findComment-75157 Share on other sites More sharing options...
akrytus Posted August 15, 2006 Share Posted August 15, 2006 He is using the random command:syntax: rand(from, to);He is using count command: this counts the number of elements in the array $images -1 becuase its starts counting at 0.Thus randomly pick a number from 0 to the number of images -1 Quote Link to comment https://forums.phpfreaks.com/topic/17633-random-file-select/#findComment-75161 Share on other sites More sharing options...
Daniel0 Posted August 15, 2006 Share Posted August 15, 2006 Exactly. And this line: [code] $images[] = $file;[/code] adds the files to an array.And another way to do this line: [code]$random_file = $images[rand(0,count($images)-1)];[/code] would be: [code]$max_num = count($images)-1;$random_number = rand(0,$max_num);$random_file = $images[$random_number];[/code] but the first way is shorter. Quote Link to comment https://forums.phpfreaks.com/topic/17633-random-file-select/#findComment-75163 Share on other sites More sharing options...
redarrow Posted August 15, 2006 Share Posted August 15, 2006 thank you all cheers. Quote Link to comment https://forums.phpfreaks.com/topic/17633-random-file-select/#findComment-75165 Share on other sites More sharing options...
Stuve Posted August 15, 2006 Author Share Posted August 15, 2006 Thanx a lot!! :DBut one more question.. how to get [b]opendir('.');[/b] to open files in a subfolder?? Quote Link to comment https://forums.phpfreaks.com/topic/17633-random-file-select/#findComment-75169 Share on other sites More sharing options...
Daniel0 Posted August 15, 2006 Share Posted August 15, 2006 [quote author=Stuve link=topic=104358.msg416238#msg416238 date=1155655614]Thanx a lot!! :DBut one more question.. how to get [b]opendir('.');[/b] to open files in a subfolder??[/quote]You would need recursion. Example:[code]<?phpheader("Content-type: text/plain");function read_contents($directory='.'){ if(substr($directory,-1) != '/') { $directory .= "/"; } $contents = @scandir($directory); if(is_array($contents)) { foreach($contents as $item) { if($item != '.' && $item != '..') { echo "{$directory}{$item}\n"; if(is_dir($directory.$item)) { read_contents($directory.$item); } } } }}read_contents("/home/daniel");?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17633-random-file-select/#findComment-75179 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.