Page 1 of 1

readdir loop into an array();

Posted: Thu Jul 08, 2004 10:32 pm
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]

Posted: Thu Jul 08, 2004 10:57 pm
by feyd
you should probably move the call to printFileArray() outside of the while loop..

Ah man... I"m such a n00b

Posted: Fri Jul 09, 2004 7:20 am
by neophyte
and there it was staring at me big goooglie eyes....

Thanks Feyd