getFileList( $t->get"/> getFileList( $t->get">

Home  >  Article  >  Backend Development  >  From now on you will be all PHP classes that list all files in a specified directory in a tree structure

From now on you will be all PHP classes that list all files in a specified directory in a tree structure

WBOY
WBOYOriginal
2016-07-29 08:34:191096browse

//List all the files in the specified directory in a tree structure. If you want to know what subdirectories and files are in a certain directory, you can call this class to view it, which is very convenient.
  # Demonstration example:
  $t = new TreeClimber( "asp" ); // Create a new object and set the directory to be listed: here is the asp directory
  echo arrayValuesToString( $t->getFileList( $t-> ;getPath() ), "
n" );
function arrayValuesToString( $ar, $nl="", $dolast=true ) {//Call function
$str = "";
reset( $ar );
$size = sizeof( $ar );
$i = 1;
while( list( $k, $v ) = each( $ar ) ) {
if ( $dolast == false ) {
if ( $i < $size ) {
              $str .= $ar[$k]  .$nl; $str , Climber {
var $path ;
var $fileList = array();
function TreeClimber( $path = "." ) {
$this->path = $path;
}
# Access path
function getPath() { return $this- >path; }
function setPath( $v ) { $this->path = $v; }
// Returns the file list in the specified directory. If no directory is specified, the current directory will be used
// If it cannot be opened Directory (may not have permission or the directory does not exist, it will be returned as empty
//Proceed in a recursive way
function getFileList( $dirname=null, $returnDirs=false, $reset=true) {
if ( $dirname == null ) { $dirname = $this->path; }
# else { $this->setPath( $dirname ); }
# dout( "Recursing into $dirname..." );
if ( $reset ) {
$this->fileList = array();
$dir = opendir( $dirname );
if ( ! $dir ) { print( " Note: TreeClimber.getFileList( $dirname ): Cannot open $dirname!" );
  return null;
}
while( $file = readdir( $dir ) ) {
if ( ereg( "^.$", $file ) || ereg( "^..$", $file ) ) continue;
if ( is_dir( $dirname."/".$file ) ) {
$this->getFileList ( $dirname."/".$file, $returnDirs, false );
if ( $returnDirs ) { $this->fileList[] = $dirname."/".$file;}
}
else { $ This-& gt; filelist [] = $ diRNAME. "/". $ File;}}
sort ($ this- & gt; filelist);
?>

The above introduces all PHP classes that list all the files in the specified directory in a tree structure, including all aspects of "You Will Be From Now On". I hope it will be helpful to friends who are interested in PHP tutorials.


Statement:
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