getting files and directorys - putting in 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
mr_malee
Forum Newbie
Posts: 1
Joined: Fri May 26, 2006 3:16 am

getting files and directorys - putting in array

Post 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 :(
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Convert your array to a string, and loop through it.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

my posts in the following thread may be of interest.

viewtopic.php?t=47288
Post Reply