Page 1 of 1

PHP function for reading filenames into array?

Posted: Tue Mar 04, 2003 2:43 pm
by crimius
I'm trying to locate a PHP function that will read all the filenames (like filename.jpg) in a particular directory into an array.

What I really want is a function that will look at all the jpegs that I have in one directory, pick a random one to write into an <img src="filename.jpg"> so my page will display a random jpeg every time it loads.

Thanks in advance,
-Craig

Posted: Tue Mar 04, 2003 3:00 pm
by BDKR
Why not write it yourself? I'm willing to bet there are enough examples in the user comments in the PHP manual to give you a real good headstart. Once you have some probs, then post a question.

Cheers,
BDKR

Posted: Tue Mar 04, 2003 3:10 pm
by crimius
Thanks for the suggestion. I will take a crack at it, but didn't want to create a function if it already existed.

Thanks again,
-Craig

Posted: Wed Mar 05, 2003 4:42 am
by jiop
crimius wrote:Thanks for the suggestion. I will take a crack at it, but didn't want to create a function if it already existed.

Thanks again,
-Craig


hmm this doesn't relate to the method that you describe, but this is how i do my random image generating.

i just name my files with the same beginning ie "iomXX.jpg" where XX is a number.

i just generate a random number using the rand function.

Code: Select all

<?php 
		$img_num = rand(1, 8);
		echo "<img src="img/iom".$img_num.".jpg" width="250" height="500">";
?>
where iom1.jpg is the first image and iom8.jpg is the last.

Posted: Wed Mar 05, 2003 5:03 am
by patrikG

Code: Select all

<?php
function getDirContent($path) 
	{
	if(is_dir($path))
		{
		exec("ls ".$path,$ls);
		foreach($ls as $key=>$value) 
			{
			if (is_dir($path."/".$value))
				$subdirectories[] = $value;
			else
				$files[]	=	$value;
				
			}
      return($files)
		}
         return false;
	}	

?>

Posted: Wed Mar 05, 2003 7:19 am
by lazy_yogi
you'd have a problem with that function getDirContent if not using linux
as there is no 'ls' command on windows. I've found some problems with that from changin between linux and windows and finally just wrote it all in php (pasted below)

Code: Select all

$files = getAllFiles(".");
while (list ($key, $val) = each ($files)) 
    echo "$val<BR>\n";


function getAllFiles ($dir) &#123;
      $single_files = array();
      $d = opendir ("$dir");

      //list the files in the dir
      while ($file = readdir ($d)) &#123;
            if (! $file == ".." && ! $file == "." && ! is_dir ($dir."/".$file))
                  $single_files&#1111;] = $file;
      &#125;

      closedir ($d);
      return $single_files;
&#125;

but I think jiop's idea is ahead of the game. Excellent idea !

Posted: Wed Mar 05, 2003 12:46 pm
by crimius
Here's what I came up with. It seems to work correctly except that, about 20% of the time, I don't get a value at all for $random_file.

Code: Select all

srand((double) microtime() * 1000000);
	$directory = '/usr/www/users/crimius/headers';
	$directory_stream = opendir($directory) or die ("Could not open a directory stream for <i>$directory</i>");
	
	while ($file = readdir($directory_stream)) 
	{
   		if (!is_dir($file)) 
		{
       		$list_of_files[] = $file;
   		}
	}
	
	closedir($directory_stream);
	$random_file = $list_of_files[rand(0,count($list_of_files))];
	
	echo "<td colspan="2"><img src="/headers/".$random_file."" width="700" height="150" vspace="3" border="0"></td>";
?>
I can't figure out why the html generated at the end looks like this about 20% of the time:

Code: Select all

<td colspan="2"><img src="/headers/" width="700" height="150" vspace="3" border="0"></td>
A filename is not generated for $random_file. It just comes up blank. There are no other files in that directory, hidden or otherwise, that are anything but jpegs. In this particular case, there are 4 jpegs there. Can anyone see any problems with this code?

Thanks in advance,
-Craig

Posted: Wed Mar 05, 2003 12:58 pm
by crimius
Hmm, I think I found my problem. I need to subtract 1 from the result of count($list_of_files) like so:

Code: Select all

$random_file = $list_of_files[rand(0,((count($list_of_files))-1))];

Posted: Wed Mar 05, 2003 1:47 pm
by pootergeist
array_rand($array_name);

much nicer way of doing it.

while ($file = readdir($directory_stream))
{
$f_type = @getimagesize($file);
if($f_type[2] == 2)
{
$list_of_files[] = $file;
}
}
echo '<img src="'. $directory. '/'. array_rand($list_of_files). '">';

Posted: Wed Mar 05, 2003 8:53 pm
by crimius
hey pooter - thanks for the clue-in on the getimagesize function. that is one cool time-saver.