Home > Backend Development > PHP Tutorial > php displays all subfolders of the specified directory_PHP tutorial

php displays all subfolders of the specified directory_PHP tutorial

WBOY
Release: 2016-07-13 10:58:40
Original
958 people have browsed it

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631996.htmlTechArticlephp tutorial displays all subfolders of the specified directory function ListFolders() { // Set a path $dir = ./ ; //Set an array to store files $aDir= array() ; $oCurrentdir = ope...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template