使用WP_Filessystem列出目录中的文件

时间:2014-09-09 作者:Rizzo

是否有一种方法可以使用WP\\u文件系统而不是类似以下内容列出目录中的文件:

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\');
我想列出“上载”文件夹中的所有文件和文件夹,所以我写的是:

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\']);
但我想看看是否有更好的方法。。

1 个回复
最合适的回答,由SO网友:Nicolai Grossherr 整理而成

不幸的是Filesystem API 是非常基本的。你要找的是dirlist 方法/功能WP_Filesystem_$method 类别。关于它的唯一文档是docblock 超出WP_Filesystem_Base 类别:

/**
 * 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;
 }
在基类中,方法实际上没有功能,为此,请查看(例如)的方法声明dirlist 内部WP_Filesystem_Direct

现在让我们来看看如何使用它,一个基本示例如下所示:

global $wp_filesystem;
$filelist = $wp_filesystem->dirlist( \'/the/path/to/the/files/\' );

结束