简体   繁体   中英

How to list directories, sub-directories and all files in php

I want to be able to list all the directories, subdirectories and files in the "./" folder ie the project folder called fileSystem which contains this php file scanDir.php.

You can view the directory system I've got here: 在此处输入图片说明

At the minute it will only return the subdirectory folders/files in the root of the mkdir directory but not any folders inside that subdirectory.

How do I modify the code so that it demonstrates all the files, directories, subdirectories and their files and subdirectories within the fileSystem folder given that the php file being run is called scanDir.php and the code for that is provided below. Here is the php code:

 $path = "./";

 if(is_dir($path))

{
    $dir_handle = opendir($path);

    //extra check to see if it's a directory handle.
    //loop round one directory and read all it's content.
    //readdir takes optional parameter of directory handle.
    //if you only scan one single directory then no need to passs in argument.
    //if you are then going to scan into sub-directories the argument needs 
    //to be passed into readdir.
    while (($dir = readdir($dir_handle))!== false) 
    {
    if(is_dir($dir))
    {
    echo "is dir: " . $dir . "<br>"; 


    if($dir == "mkdir") 
        {
        $sub_dir_handle = opendir($dir);
        while(($sub_dir = readdir($sub_dir_handle))!== false)
            {
            echo "--> --> contents=$sub_dir <br>";
            }
    }



    }    
        elseif(is_file($dir)) 
         {
            echo "is file: " . $dir . "<br>"  ;
        }
    }
closedir($dir_handle); //will close the automatically open dir.
}

else {

    echo "is not a directory";
}

Use scandir to see all stuff in the directory and is_file to check if the item is file or next directory, if it is directory, repeat the same thing over and over.

So, this is completely new code.

function listIt($path) {
$items = scandir($path);

foreach($items as $item) {

    // Ignore the . and .. folders
    if($item != "." AND $item != "..") {
        if (is_file($path . $item)) {
            // this is the file
            echo "-> " . $item . "<br>";
        } else {
            // this is the directory

            // do the list it again!
            echo "---> " . $item;
            echo "<div style='padding-left: 10px'>";
            listIt($path . $item . "/");
            echo "</div>";
        }
    }
  }
}

echo "<div style='padding-left: 10px'>";
listIt("/");
echo "</div>";

You can see the live demo here in my webserver, btw, I will keep this link just for a second

When you see the "->" it's an file and "-->" is a directory

The pure code with no HTML:

function listIt($path) {
$items = scandir($path);

foreach($items as $item) {
    // Ignore the . and .. folders
    if($item != "." AND $item != "..") {
        if (is_file($path . $item)) {
            // this is the file
            // Code for file
        } else {
            // this is the directory
            // do the list it again!
            // Code for directory
            listIt($path . $item . "/");
        }
    }
  }
}

listIt("/");

the demo can take a while to load, it's a lot of items.

There are some powerful builtin functions for PHP to find files and folders, personally I like the recursiveIterator family of classes.

$startfolder=$_SERVER['DOCUMENT_ROOT'];
$files=array();


foreach( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $startfolder, RecursiveDirectoryIterator::KEY_AS_PATHNAME ), RecursiveIteratorIterator::CHILD_FIRST ) as $file => $info ) {
    if( $info->isFile() && $info->isReadable() ){
        $files[]=array('filename'=>$info->getFilename(),'path'=>realpath( $info->getPathname() ) );
    }
}

echo '<pre>',print_r($files,true),'</pre>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM