I just want to scan through a directory containing several subdirectories, each containing several text files, and display the name/word count/character count of each text file in each subdirectory.

All I'm looking for is a quick and simple way to do the scanning through all subdirectories finding each file.

thanks in advance

Hi
I think your looking for something like this:

<?php
$dir    = '/directory to be scanned';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);

print_r($files1);
print_r($files2);
?>

Obviouslt you'l want to clean up the array and echo it in a better fashon but print_r does the job to show you whats happening.

Hope this helps


Have I helped? Add to my reputation, I need it :(

I'm afraid that's not what I'm looking for, it only lists the names of any files and subdirectories in the directory specified.

I'm trying to list the names of all files within all subdirectories of the specified directory.

thanks for the reply though!

Here's a bit of code that might help:

<?php

function recurseDir($dir){
     		if(is_dir($dir)){
 				if($dh = opendir($dir){
	 				while(($file = readdir($dh)) !== false){
		 				if($file != '.' && $file != '..'){
			 			     if(!is_dir($dir . $file)){
				 			  	echo $dir . $file;   
			 			     }else{
                                                            echo $dir . $file;
                                                            // since it is a directory we recurse it.
				 			    recurseDir($dir . $file); 
			 			     }	
		 				}
	 				}
 				}
 				closedir($dh);         
     		}
	}

?>

This should go through each directory and display all files

Fixed. And resurrecting a year old thread :D

function recurseDir($dir) {
	if(is_dir($dir)) {
		if($dh = opendir($dir)){
			while($file = readdir($dh)){
				if($file != '.' && $file != '..'){
					if(is_dir($dir . $file)){
						echo $dir . $file;
						// since it is a directory we recurse it.
						recurseDir($dir . $file . '/');
					}else{
						echo $dir . $file;   
			 		}
				}
	 		}
		}
 		closedir($dh);         
     	}
}

Edited 6 Years Ago by HavocAngel: n/a

This article has been dead for over six months. Start a new discussion instead.