readdir loop into an 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
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

readdir loop into an array();

Post by neophyte »

I'm trying to write a class that will read a directory into an array that I can then use for other purposes.

Code: Select all

class displayImages {
	var $directoryName;
	var $file;
	var $file_array = array();
		function displayImages ($name ="item"){
			$this->directoryName = $name;
			$dh = opendir ($this->directoryName);
				while (false !== ($this->file = readdir($dh))){
					
					if ($this->file != "." && $this->file != ".."){
						$this->file_array[] = $this->file;
					}
					$this->printFileArray();
					
				
				}
		}

		function printFileArray (){
		sort($this->file_array);
		foreach($this->file_array as $val){
		$length = count($this->file_array);
		print "THis array is $length";	
				print $val."<br>\n";
				
		}
		
		
		
		}
		
		
		
}
$display = new displayImages("reunion");
I am successfully creating the array but when I loop through them I get unexpected results. When I step through the array using the "foreach" statement above this is what gets printed to the screen:

Code: Select all

THis array is 1DSCN0341.jpg
THis array is 2DSCN0341.jpg
THis array is 2reunionhi
THis array is 3DSCN0341.jpg
THis array is 3DSCN0342.jpg
THis array is 3reunionhi
THis array is 4DSCN0341.jpg
THis array is 4DSCN0342.jpg
THis array is 4DSCN0343.jpg
THis array is 4reunionhi
THis array is 5DSCN0341.jpg
THis array is 5DSCN0342.jpg
THis array is 5DSCN0343.jpg
THis array is 5DSCN0344.jpg
THis array is 5reunionhi
THis array is 6DSCN0341.jpg
THis array is 6DSCN0342.jpg
THis array is 6DSCN0343.jpg
THis array is 6DSCN0344.jpg
THis array is 6DSCN0345.jpg
THis array is 6reunionhi
There's more than that but you can see the pattern. What does this mean? and how can I rewrite the script to store the directory information in an indexed array?

Thanks


feyd | switched around

Code: Select all

and [quote] tags a bit..[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you should probably move the call to printFileArray() outside of the while loop..
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Ah man... I"m such a n00b

Post by neophyte »

and there it was staring at me big goooglie eyes....

Thanks Feyd
Post Reply