Link to home
Start Free TrialLog in
Avatar of theredcode
theredcode

asked on

PHP Code : How to scan a folder for all folders in it and then print their sorted names in numbered list ?

Hi

We need to scan a folder for all other folders that a re present in it. It should scan to very deep level of folders.

It then, should print sorted, numbered listed of all folders found.

 It should also ignore default "." & ".." directories.

Thanks,
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

please check out this function:
http://www.bin-co.com/php/scripts/filesystem/ls/
Avatar of afzz
afzz

try this
<?php
function showDirect( $pth = '.', $lev = 0 ){
    $ignore = array( 'cgi-bin', '.', '..' );// Directories to ignore
    $dh = @opendir( $pth );
    while( false !== ( $file = readdir( $dh ) ) ){
        if( !in_array( $file, $ignore ) ){
            $spacer = str_repeat( '&nbsp;', ( $lev * 4 ) );
            if( is_dir( "$pth/$file" ) ){
                echo "<strong>$spacer $file</strong><br />";
                showDirect( "$pth/$file", ($lev+1) );            
            } else {
                echo "$spacer $file<br />";            
            }
        }
    }
    closedir( $dh );
}
 
?> 
 
 
Calling the function
 
 
showDirect( "." );// Current directory
 
showDirect( "./this/directory" );// Show "this/directory"

Open in new window

Avatar of theredcode

ASKER

not working afzz....

it will print the directory contents recursively. the spacer variable needs to be modified to output as a list

check this page

https://www.experts-exchange.com/questions/23485982/How-to-scan-all-directories-and-sub-directories-and-print-their-name-with-numbering-link-to-them.html
still strugling...

cant make numbered list and i also unable to print number of files found in each directory in line under it. Also sorted list.

what i want is...

1. Audio
Files Fould : 5

2. English
Files Fould : 0

3. Audio
Files Fould :8

4. English
Files Fould :1
sorry in alphabatically sorted list
ASKER CERTIFIED SOLUTION
Avatar of artzter
artzter
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can use this:
<?php
echo '<ul>';
listFiles('/path/to/my/folder/');
echo '</ul>';
 
function listFiles($dir) {
	$it    = opendir($dir);
	$files = 0;
	$dirs  = array();
	while($f = readdir($it)) {
		if($f == '.' || $f == '..') {
			continue;
		}
		if(is_file($dir.'/'.$f)) {
			$files++;
		} else {
			$dirs[] = $dir.'/'.$f;
		}
	}
	
	echo '<li>'.basename($dir);
	echo '<br />Files Found: '.$files;
	if(count($dirs) > 0) {
		echo '<ul>';
		array_map('listFiles', $dirs);
		echo '</ul>';
	}
	echo '</li>';
}

Open in new window

Thanks very much