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

Dani AI

Generated

If you want a simple, robust way to recurse through all subdirectories and handle only text files, PHP’s SPL iterators make this very straightforward. Unlike scandir(), they traverse directories recursively for you. The example below prints each .txt file’s relative path, word count, and character count.

$root = '/path/to/root';

$it = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS)
);

foreach ($it as $file) {
    if (!$file->isFile()) continue;
    if (strtolower($file->getExtension()) !== 'txt') continue;
    if (!$file->isReadable()) continue;

    $contents = file_get_contents($file->getPathname());
    if ($contents === false) continue;

    $words = str_word_count($contents);
    $chars = strlen($contents); // for UTF-8, consider mb_strlen($contents, 'UTF-8')
    printf("%s | words: %d | chars: %d\n", $it->getSubPathname(), $words, $chars);
}

Notes:

  • By default, symlinks are not followed, which helps avoid infinite loops. You can change that with FilesystemIterator::FOLLOW_SYMLINKS if needed. See RecursiveDirectoryIterator and RecursiveIteratorIterator.
  • For large files, reading them fully with file_get_contents() can be memory-heavy. If that is a concern, stream the file in chunks and approximate word counts, or constrain by size first ($file->getSize()).
  • If you need to verify files are text rather than relying on the .txt extension, detect mime type via finfo (docs). For multibyte character counts, prefer mb_strlen (docs). For word counts, str_word_count (docs) is quick for English-like text.

Recommended Answers

All 5 Replies

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!

This page should help, have a look at some of the functions and put it into your own if you like.

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);         
     	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.