php tutorial display all subfolders of the specified directory
function ListFolders()
{
//Set a path
$dir = "./";
//Set an array to store files
$aDir = array() ;
$oCurrentdir = opendir( $dir ); //Open directory
while ( $sFile = readdir( $oCurrentdir ) ) //Read all subdirectories of the current directory
{
if ( $sFile != '.' && $sFile != '..' && is_dir( $dir . $sFile ) )//Determine whether it is the root directory
$aFolders[] = 'Directory:' .$sFile. '
' ;//Input directory
}
closedir( $oCurrentdir ) ;//Judge the directory just opened with opendir
// Open the "Folders" node.
natcasesort($aFolders); //Naturally sort the array. The case-insensitive natural order algorithm sorts the elements in the given array, that is, the sorting method of numbers from 1 to 9, the sorting method of letters from a to z, whichever is shorter Priority
foreach ( $aFolders as $sFolder )
{
echo $sFolder ;
}
}
/*
Test, we have two subdirectories in the current directory
Directory:1
Directory: www.bKjia.c0m
*/
getFolders();
/*
After running, the result is
Directory:1
Directory: www.bKjia.c0m
Common function analysis:
array() data declaration, you can use direct assignment array(1,2,3,4);
opendir() opens a directory. The opendir() function opens a directory handle and can be used by closedir(), readdir() and rewinddir(). If successful, the function returns a directory stream, otherwise it returns false and an error. You can hide error output by prepending "@" to the function name.
readdir reads directory
cosedir judgment directory
The natcasesort() function sorts the elements in the given array using a case-insensitive natural ordering algorithm. The natcasesort() function implements "natural sorting", that is, the sorting method of numbers from 1 to 9, and the sorting method of letters from a to z. The shorter one is given priority. This function is not case-sensitive. The index of the array remains associated with the cell value. The function returns TRUE if successful, FALSE otherwise.
The compilation and original reprinting of this website indicate that it comes from www.bKjia.c0m