PHP function for reading filenames into array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crimius
Forum Commoner
Posts: 28
Joined: Sun Oct 13, 2002 6:02 pm
Location: Austin, Texas
Contact:

PHP function for reading filenames into array?

Post 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
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
crimius
Forum Commoner
Posts: 28
Joined: Sun Oct 13, 2002 6:02 pm
Location: Austin, Texas
Contact:

Post 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
jiop
Forum Newbie
Posts: 11
Joined: Wed Dec 18, 2002 4:00 am
Location: newport beach

Post 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.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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;
	}	

?>
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post 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 !
crimius
Forum Commoner
Posts: 28
Joined: Sun Oct 13, 2002 6:02 pm
Location: Austin, Texas
Contact:

Post 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
crimius
Forum Commoner
Posts: 28
Joined: Sun Oct 13, 2002 6:02 pm
Location: Austin, Texas
Contact:

Post 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))];
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post 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). '">';
crimius
Forum Commoner
Posts: 28
Joined: Sun Oct 13, 2002 6:02 pm
Location: Austin, Texas
Contact:

Post by crimius »

hey pooter - thanks for the clue-in on the getimagesize function. that is one cool time-saver.
Post Reply