Page 1 of 1

getting files and directorys - putting in array

Posted: Fri May 26, 2006 3:32 am
by mr_malee
hello all, i'm a total noob to php, just thought i might give it a go. Anyway i want my php file to create an array, search through its current directory, find the sub directorys and then add the files in those directorys to an array.

something like this:

dirArray = [ directory1 [file1,file2,file3,] , directory2 [file1,file2,file3] , directory3 [file1,file2,file3] ]

my php code is: (i seriously just started today, went to w3schools and read through a bit of syntax, so my code might be a little crappy ;))

Code: Select all

<?php

$dirArray = array();
$checkArray = scandir(getcwd());

clearstatcache();

//add directorys to dirArray
foreach ($checkArray as $value){
	$isDirectory = is_dir($value);
	if($isDirectory && $value != "." && $value != ".."){
		array_push ($dirArray, $value);			
	}
}
//add files to the correct directory in dirArray (not working)
foreach ($dirArray as $directory){
	$files = scandir($directory);
	$directory = array();
	foreach ($files as $file){	
		if(!is_dir($file)){
			array_push($directory, $file);
		}
	}
}
//print the array
print_r($dirArray);

?>
it gets the directoys and files but at the end it only returns dirArray as

Array ( [0] => directory1[1] => directory2[2] => directory3[3] => directory4)

it misses out the sub directorys. In actionscript i can do it so easily, but with php i can't :(

Posted: Fri May 26, 2006 6:56 am
by GeXus
Convert your array to a string, and loop through it.

Posted: Fri May 26, 2006 8:37 am
by feyd
my posts in the following thread may be of interest.

viewtopic.php?t=47288