Jump to content


Photo

Random file select


  • Please log in to reply
7 replies to this topic

#1 Stuve

Stuve
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 15 August 2006 - 02:55 PM

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.

<?php
$rep=opendir('.');

while ($file = readdir($rep)) {
	if ($file != '..' && $file !='.' && $file !='' && $file !=is_dir($file)) {
		echo "<img src=\"$file\">";
	}
}

closedir($rep);
clearstatcache();
?>

Thanx!!

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 15 August 2006 - 03:04 PM

A modification of your 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' />";
?>


#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 15 August 2006 - 03:08 PM


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)];
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 akrytus

akrytus
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 15 August 2006 - 03:16 PM

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


#5 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 15 August 2006 - 03:18 PM

Exactly. And this line:
$images[] = $file;
adds the files to an array.

And another way to do this line:
$random_file = $images[rand(0,count($images)-1)];
would be:
$max_num = count($images)-1;
$random_number = rand(0,$max_num);
$random_file = $images[$random_number];
but the first way is shorter.

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 15 August 2006 - 03:22 PM

thank you all cheers.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#7 Stuve

Stuve
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 15 August 2006 - 03:26 PM

Thanx a lot!! :D

But one more question.. how to get opendir('.'); to open files in a subfolder??

#8 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 15 August 2006 - 03:50 PM

Thanx a lot!! :D

But one more question.. how to get opendir('.'); to open files in a subfolder??


You would need recursion. Example:
<?php
header("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");
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users