PHP shows all subfolders of the specified directory

Source: Internet
Author: User

function Listfolders ()
{
Set a path
$dir = "./";

Set up an array of file pieces
$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))//To determine whether it is the root directory
$aFolders [] = ' directory: '. $sFile. ' <br/> '/The list of the Lost
}

Closedir ($oCurrentdir);//Judge the directory you just opened with Opendir

Open the "Folders" node.


Natcasesort ($aFolders); Natural order case-insensitive for an array sorting the elements in the given arrays, that is, sorting the numbers from 1 to 9, sorting the letters from A to Z, the short priority
foreach ($aFolders as $sFolder)
{
Echo $sFolder;
}


}

/*
Test, we have two subdirectories in the current directory

Table of Contents: 1
Table of Contents: www.111cn.net


*/
Getfolders ();

/*
After running, the results are

Table of Contents: 1
Table of Contents: www.111cn.net

Common function parsing:
Array () data declaration, you can use Direct Assignment array (1,2,3,4);
Opendir () Opens the directory, and the Opendir () function opens a directory handle that can be used by Closedir (), Readdir (), and Rewinddir () and, if successful, returns a directory stream, or false and an error. You can hide the output of an error by adding "@" in front of the function name.

Readdir Read Directory
Cosedir Judgment Catalogue
The Natcasesort () function sorts the elements in a given array with a case-insensitive natural order algorithm. The Natcasesort () function implements a "natural sort", the sort method of numbers from 1 to 9, the sorting method of letters from A to Z, the short precedence, and the function is case-insensitive. The index of an array remains associated with the cell value. If successful, the function returns TRUE, otherwise it returns FALSE.

Site finishing and original reprint annotated from Www.111cn.net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.