Home > Backend Development > PHP Tutorial > PHP lists all subfolders and file instances of a specified directory_PHP tutorial

PHP lists all subfolders and file instances of a specified directory_PHP tutorial

WBOY
Release: 2016-07-13 10:58:42
Original
870 people have browsed it

php tutorial lists all subfolders and file instances of a specified directory
function listDirFiles()
{
// Set directory
$dirs = './';

// Declare folder array and file array
$aFolders = array() ;
$aFiles = array() ;

$ocfolder = opendir( $dirs ) ;//The function returns a directory stream, otherwise it returns false and an error. You can hide error output by prepending "@" to the function name. Usage opendir(path,context)


while ( $sFile = readdir( $ocfolder ) ) // The readdir() function returns the entry in the directory handle opened by opendir() Syntax: readdir(dir_stream)
{
if ( $sFile != '.' && $sFile != '..' )
{
If ( is_dir( $dirs . $sFile ) ) // Determine whether it is a directory, return true if it is, otherwise return false;
$aFolders[] = 'dirs:' . $sFile . '
' ;
else
{
$fsize = @filesize( $dirs . $sFile ) ; //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 floating point numbers. round(x,prec)
If ( $fsize < 1 ) $fsize = 1 ;
}

$aFiles[] = 'File name:' .$sFile . 'File size=' . $fsize . 'KB
' ;
}
}
}


//Naturally sort the directory

natcasesort( $aFolders ) ;//Naturally sort the array natcasesort(array)

foreach ( $aFolders as $sFolder )
{
echo $sFolder ;
}

// Sort files naturally
natcasesort( $aFiles ); // Natural 1-9, a-z sorting natcasesort(array);
foreach ( $aFiles as $sFiles )
{
echo $sFiles ;
}


}

/*
The calling method currently has 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

Use function analysis
opendir
then the function returns a directory stream, otherwise it returns false and an error. You can hide error output by prepending "@" to the function name. Usage opendir(path,context)
is_dir
Determine whether it is a directory, return true if it is, otherwise return false;
readdir
The readdir() function returns the entry in the directory handle opened by opendir() Syntax: readdir(dir_stream)
filesize()
The function returns the size of the specified file. Syntax: filesize(filename)
natcasesort(array)
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 is associated with the cell value

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631995.htmlTechArticlephp tutorial lists all subfolders and file instances of the specified directory function listDirFiles() { // Set directory $dirs = './'; // Declare folder array and file array $aFolders= array() ; $a...
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