0

Is there a way to list files in a directory using WP_filesystem instead of something like:

function listFolderFiles($dir){
    $ffs = scandir($dir);
    echo '<ol>';
    foreach($ffs as $ff){
        if($ff != '.' && $ff != '..'){
            echo '<li>'.$ff;
            if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
            echo '</li>';
        }
    }
    echo '</ol>';
}

listFolderFiles('Main Dir');

I want to list all the files and folders in my uploads folder so I was writing something like:

class ff
{
    public static function listFolderFiles($dir)
    {
        $ffs = scandir($dir);
        echo '<ol>';
        foreach($ffs as $ff){
            if($ff != '.' && $ff != '..'){
                echo '<li>'.$ff;
                if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
                echo '</li>';
            }
        }
        echo '</ol>';
    }
}
$upload_dir = wp_upload_dir(); 
return New ff::listFolderFiles($upload_dir['basedir']);

but wanted to see if there is a better way..

1

Unfortunately the documentation about the Filesystem API is very basic. What you are looking for is the dirlist method/function of the WP_Filesystem_$method class(es). The only documentation about it is the docblock out of the WP_Filesystem_Base class:

/**
 * Get details for files in a directory or a specific file.
 *
 * @since 2.5.0
 *
 * @param string $path Path to directory or file.
 * @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
 *                             Default true.
 * @param bool $recursive Optional. Whether to recursively include file details in nested directories.
 *                        Default false.
 * @return array|bool {
 *     Array of files. False if unable to list directory contents.
 *
 *     @type string 'name' Name of the file/directory.
 *     @type string 'perms' *nix representation of permissions.
 *     @type int 'permsn' Octal representation of permissions.
 *     @type string 'owner' Owner name or ID.
 *     @type int 'size' Size of file in bytes.
 *     @type int 'lastmodunix' Last modified unix timestamp.
 *     @type mixed 'lastmod' Last modified month (3 letter) and day (without leading 0).
 *     @type int 'time' Last modified time.
 *     @type string 'type' Type of resource. 'f' for file, 'd' for directory.
 *     @type mixed 'files' If a directory and $recursive is true, contains another array of files.
 * }
 */
 public function dirlist( $path, $include_hidden = true, $recursive = false ) {
     return false;
 }

In the base class the method doesn't actually have the functionality, for that take a look - for example - into the method declaration of dirlist inside the WP_Filesystem_Direct class.

Now lets get to how to use it, a basic example would look like this:

global $wp_filesystem;
$filelist = $wp_filesystem->dirlist( '/the/path/to/the/files/' );
  • This worked reallny nice: stevenmcmillan.co.uk/blog/2011/… I will look into the way you described Thanks so much! – Rizzo Sep 9 '14 at 19:44
  • My pleasure! Sure you can do it with pure PHP, but than it wouldn't be based on the WP Filesystem, right? ;) @Rizzo – Nicolai Sep 9 '14 at 19:46
  • WordPress now has a bit more documentation on this: codex.wordpress.org/Filesystem_API Updated your splendid answer to reflect this – kakoma Feb 17 '17 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.