Php list files in directory and subdirectories

Late to the show, but to build off of the accepted answer...

If you want to have all the files and directories in as array [that can be prettied-up nicely with JSON.stringify in javascript], you can modify the function to:

function listFolderFiles[$dir] { 
    $arr = array[];
    $ffs = scandir[$dir];

    foreach[$ffs as $ff] {
        if[$ff != '.' && $ff != '..'] {
            $arr[$ff] = array[];
            if[is_dir[$dir.'/'.$ff]] {
                $arr[$ff] = listFolderFiles[$dir.'/'.$ff];
            }
        }
    }

    return $arr;
}

For the Newbies...

To use the aforementioned JSON.stringify, your JS/jQuery would be something like:

var ajax = $.ajax[{
    method: 'POST',
    data: {list_dirs: true}
}].done[function[msg] {
    $['pre'].html[
        'FILE LAYOUT
' + JSON.stringify[JSON.parse[msg], null, 4] ]; }];

^ This is assuming you have a

 element in your HTML somewhere. Any flavour of AJAX will do, but I figure most people are using something
similar to the jQuery above.

And the accompanying PHP:

if[isset[$_POST['list_dirs']]] {
    echo json_encode[listFolderFiles[$rootPath]];
    exit[];
}

where you already have listFolderFiles from before.

In my case, I've set my $rootPath to the root directory of the site...

$rootPath; 
if[!isset[$rootPath]] {
    $rootPath = $_SERVER['DOCUMENT_ROOT'];
}

The end result is something like...

|    some_file_1487.smthng    []
|    some_file_8752.smthng    []
|    CSS    
|    |    some_file_3615.smthng    []
|    |    some_file_8151.smthng    []
|    |    some_file_7571.smthng    []
|    |    some_file_5641.smthng    []
|    |    some_file_7305.smthng    []
|    |    some_file_9527.smthng    []
|    
|    IMAGES    
|    |    some_file_4515.smthng    []
|    |    some_file_1335.smthng    []
|    |    some_file_1819.smthng    []
|    |    some_file_9188.smthng    []
|    |    some_file_4760.smthng    []
|    |    some_file_7347.smthng    []
|    
|    JSScripts    
|    |    some_file_6449.smthng    []
|    |    some_file_7864.smthng    []
|    |    some_file_3899.smthng    []
|    |    google-code-prettify    
|    |    |    some_file_2090.smthng    []
|    |    |    some_file_5169.smthng    []
|    |    |    some_file_3426.smthng    []
|    |    |    some_file_8208.smthng    []
|    |    |    some_file_7581.smthng    []
|    |    |    some_file_4618.smthng    []
|    |    
|    |    some_file_3883.smthng    []
|    |    some_file_3713.smthng    []

... and so on...

Note: Yours will not look exactly like this - I've modified JSON.stringify to display tabs [vertical pipes], align all keyed values, remove quotes off of keys, and a couple other things. I will modify this answer with a link if I ever get to uploading it, or get enough interest.

[PHP 5, PHP 7, PHP 8]

scandirList files and directories inside the specified path

Description

scandir[string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, ?resource $context = null]: array|false

Parameters

directory

The directory that will be scanned.

sorting_order

By default, the sorted order is alphabetical in ascending order. If the optional sorting_order is set to SCANDIR_SORT_DESCENDING, then the sort order is alphabetical in descending order. If it is set to SCANDIR_SORT_NONE then the result is unsorted.

context

For a description of the context parameter, refer to the streams section of the manual.

Return Values

Returns an array of filenames on success, or false on failure. If directory is not a directory, then boolean false is returned, and an error of level E_WARNING is generated.

Changelog

VersionDescription
8.0.0 context is now nullable.

Examples

Example #1 A simple scandir[] example

The above example will output something similar to:

Array
[
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
]
Array
[
    [0] => somedir
    [1] => foo.txt
    [2] => bar.php
    [3] => ..
    [4] => .
]

Notes

Tip

A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen[] for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

See Also

  • opendir[] - Open directory handle
  • readdir[] - Read entry from directory handle
  • glob[] - Find pathnames matching a pattern
  • is_dir[] - Tells whether the filename is a directory
  • sort[] - Sort an array in ascending order

dwieeb at gmail dot com

10 years ago

Easy way to get rid of the dots that scandir[] picks up in Linux environments:

coolbikram0 at gmail dot com

9 months ago

A simple recursive function to list all files and subdirectories in a directory:

mmda dot nl at gmail dot com

9 years ago

Here is my 2 cents. I wanted to create an array of my directory structure recursively. I wanted to easely access data in a certain directory using foreach. I came up with the following:



Output
Array
[
   [subdir1] => Array
   [
      [0] => file1.txt
      [subsubdir] => Array
      [
         [0] => file2.txt
         [1] => file3.txt
      ]
   ]
   [subdir2] => Array
   [
    [0] => file4.txt
   }
]

info at remark dot no

4 years ago

Someone wrote that array_slice could be used to quickly remove directory entries "." and "..". However, "-" is a valid entry that would come before those, so array_slice would remove the wrong entries.

eep2004 at ukr dot net

7 years ago

Fastest way to get a list of files without dots.

Chủ Đề