Jump to content

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!!
Link to comment
https://forums.phpfreaks.com/topic/17633-random-file-select/
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]
Link to comment
https://forums.phpfreaks.com/topic/17633-random-file-select/#findComment-75153
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.
Link to comment
https://forums.phpfreaks.com/topic/17633-random-file-select/#findComment-75163
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]
Link to comment
https://forums.phpfreaks.com/topic/17633-random-file-select/#findComment-75179
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.