PHP Tutorial Lists all subfolders and file instances in the specified directory
function Listdirfiles ()
{
Set Directory
$dirs = './';
Declaring a folder array with an array of files
$aFolders = Array ();
$aFiles = Array ();
$ocfolder = Opendir ($dirs);//The function returns a directory stream, otherwise it returns false and an error. You can hide the output of the error by adding "@" before the function name. Usage Opendir (path,context)
while ($sFile = Readdir ($ocfolder))//Readdir () function returns the entry syntax in a directory handle opened by Opendir (): Readdir (Dir_stream)
{
if ($sFile! = '. ' && $sFile! = ' ... ')
{
if (Is_dir ($dirs. $sFile))//Determines whether the directory is returned by Ture no returns false;
$aFolders [] = ' dirs: '. $sFile. '
' ;
Else
{
$fsize = @filesize ($dirs. $sFile); The FileSize () function returns the size of the specified file. Syntax: filesize (filename)
if (! $fsize) {
$fsize = 0;
}
if ($fsize > 0)
{
$fsize = round ($fsize/1024); the//round () function rounds the floating-point number. Round (X,PREC)
if ($fsize < 1) $fsize = 1;
}
$aFiles [] = ' file name: '. $sFile. ' File size = '. $fsize. ' KB
' ;
}
}
}
Natural sorting of a table of contents
Natcasesort ($aFolders);//Array Natural sort Natcasesort (array)
foreach ($aFolders as $sFolder)
{
Echo $sFolder;
}
Natural sorting of files
Natcasesort ($aFiles); Natural 1-9,a-z sort Natcasesort (array);
foreach ($aFiles as $sFiles)
{
Echo $sFiles;
}
}
/*
The calling method currently has a directory
Dirs:1
dirs:www.bKjia.c0m
File name: 1.php
File name: 2.php
*/
Listdirfiles ();
/*
The output result is
Dirs:1
dirs:www.bKjia.c0m
File name: 1.php File Size =1kb
File name: 2.php File Size =2kb
Using the function parsing
Opendir
The function returns a directory stream, otherwise it returns false and an error. You can hide the output of the error by adding "@" before the function name. Usage Opendir (path,context)
Is_dir
Determines whether the directory is returned ture no returns false;
Readdir
The Readdir () function returns the entry syntax in a directory handle opened by Opendir (): Readdir (Dir_stream)
FileSize ()
The function returns the size of the specified file. Syntax: filesize (filename)
Natcasesort (Array)
The Natcasesort () function implements the "natural sort", which is a sort of number from 1 to 9, the sorting method of letters from A to Z, the shorter is preferred, the function is not case-sensitive. The index of the array remains associated with the cell value
http://www.bkjia.com/PHPjc/631995.html www.bkjia.com true http://www.bkjia.com/PHPjc/631995.html techarticle PHP Tutorial Lists all subfolders of the specified directory with file instance function Listdirfiles () {//set directory $dirs = './';//Declare folder array with file array $aFolders = array (); $a ...