Random image w/ filename as title... (solved)

images php coding issues or problems here.

Moderators: macek, egami, gesf

Post Reply
User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Random image w/ filename as title...

Post by egami » Sun Jun 19, 2011 10:44 am

Ok, so this is pretty easy.
I'm not going to write the code for you, but I will tell you the steps and functions to make this work effectively.

Step 1:
Php.net/scandir
This will list all files in the directory, and create an array.

Step 2:
Get the amount of files in your newly created array by using the count function. Php.net/count


Step 3:
Once you have the count, create a range to randomly choose from
Php.net/rand

Step 4:
Call the picture from the array and create the a link

SUMMARY:
$directory = 'this/dir';
$array = scandir($directory);
unset($array['1']);
unset($array['0']);
$counts = count($array);

$filename = rand(0,$counts);

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Random image w/ filename as title... (solved)

Post by egami » Tue Jun 21, 2011 8:11 am

You got it, pretty simple isn't it.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest