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,
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,
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( ' ', ( $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"
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
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
ASKER
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
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
ASKER
sorry in alphabatically sorted list
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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>';
}
ASKER
Thanks very much
http://www.bin-co.com/php/scripts/filesystem/ls/