Jump to content

[SOLVED] Recursive file listing of directories


Jessica

Recommended Posts

I am using this function to get all of the files in a directory, and also in any subdirectories in that directory. For the subdirectories, it needs to append the subdirectory to the file. Here is what I am using:

 

function dirList($directory, $add=''){
$results = array();
$handler = opendir($directory);

while($file = readdir($handler)){
	if($file != '.' && $file != '..'){
		if(is_dir($directory.$file)){
			$dirResults = dirList($directory.$file, $file.'/');
			$results = $dirResults+$results;
			print_r($results);
		}else{
			$results[] = $add.$file;
		}
	}
    }

    closedir($handler);
    return $results;
}

 

However, my print_r() only shows the results for the current subdirectory - just $dirResults, not $dirResults+$results.

There are four subdirectories, after the first subdirectory it should show the contents of both the first and second in results, then first, second and third, etc.

 

The final array returned has the last subdirectory and all of the files in the main folder.

 

Help Please?

Link to comment
Share on other sites

I came up with this one day when I was making a script to delete all of the files in a folder and it's subfolders.

 

function ListFiles($folder) {
$out = array();
$han = opendir($folder);
$f = (!empty($folder)) ? $folder . '/' : '';
while($it = readdir($han)) {
	if($it == '.' || $it == '..') continue;
	if(!is_dir($f.$it)) {
		$out[] = (strlen($folder) > 0) ? $folder . '/' . $it : $it;
		continue;
	}
	$path = (strlen($folder) > 0) ? $folder . '/' . $it : $it;
	$out = array_merge($out, ListFiles($path));

}
return $out;
}

$out = ListFiles('folder');

 

All it does is return an array where the values are folder/file....  Or folder/subfolder/subfolder/file so on....

 

The only problem is, it doesn't do anything with empty folders since it only keeps up with files, and that might not be the format you want.

Link to comment
Share on other sites

Appends the results from the dirList of the subdirectory to the current list of results.

 

I changed it to:

$results += dirList($directory.$file, $file.'/');

And now it shows the first subdirectory for the first print_r, and for the other three, the first TWO subdirectories only.

 

I am so confused, it has got to be something simple.

 

 

Corbin:

I don't want to use empty folders, just the files, so I'll try yours. Thanks!

Link to comment
Share on other sites

Sweet, now I get all 5 folders :) The only problem is it shows the entire directory, so I'm going to figure out some way to remove the main directory and just show the folder on the subdirectories.

 

Thanks!

 

Edit: Here's my version now. I changed the code to my style so I'd be able to read it later ;)

 

function dirList($folder, $add='') {
$out = array();
$han = opendir($folder);
$f = (!empty($folder)) ? $folder . '/' : '';
while($file = readdir($han)) {
	if($file != '.' && $file != '..'){
		if(!is_dir($f.$file)) {
			$out[] = $add.$file;
			continue;
		}
		$out = array_merge($out, dirList($folder.$file, $file.'/'));
	}
}
return $out;
}

 

Works great, I get all my folders and files. I spent an hour on this, should have come here a bit sooner ;)

Link to comment
Share on other sites

function ListFiles($folder) {
$out = array();
$han = opendir($folder);
$f = (!empty($folder)) ? $folder . '/' : '';
while($it = readdir($han)) {
	if($it == '.' || $it == '..') continue;
	if(!is_dir($f.$it)) {
		if(!empty($folder)) {
			$out[] = (strlen($folder) > 0) ? $folder . '/' . $it : $it;
		}
		continue;
	}
	$path = (strlen($folder) > 0) ? $folder . '/' . $it : $it;
	$out = array_merge($out, ListFiles($path));

}
return $out;
}

 

If that was run in /folder/, all of the subdirs of folder would be included, but the files in folder would be ignored ;p.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.