Jump to content

Archived

This topic is now archived and is closed to further replies.

Stuve

Random file select

Recommended Posts

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!!

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

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)];

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Thanx a lot!! :D

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

Share this post


Link to post
Share on other sites
[quote author=Stuve link=topic=104358.msg416238#msg416238 date=1155655614]
Thanx a lot!! :D

But one more question.. how to get [b]opendir('.');[/b] to open files in a subfolder??
[/quote]

You would need recursion. Example:
[code]<?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");
?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.