PHP lists all subfolders and file instances of the specified directory _php tutorial

Source: Internet
Author: User
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 ...

  • 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.