Concrete5 - CMS :: 以编程方式获取文件管理器文件夹中的所有文件

Concrete5 - CMS :: Get all file inside a file manager folder programmatically

我的文件夹结构如下:

folder1
    subfolder1
           file1.pdf
           file2.pdf
    subfolder2
           file3.pdf
           file4.pdf

有什么方法可以使用 "folder1" id 检索所有 pdf 文件(以编程方式)?

有,但请确保您的文件夹结构不要过分深入,您不想浏览数百个文件夹、数百个级别。

这是代码

<?php
use Concrete\Core\Tree\Node\Type\FileFolder;
use Concrete\Core\File\FolderItemList;

// First grab the folder object
$folder = FileFolder::getNodeByName('Testing Folder');

if (is_object($folder)) {
    $files = [];
    // if we have a folder we need to grab everything inside and then
    // recursively go through the folder's content
    // if what we get is a file we list it
    // otherwise if it's another folder we go through it as well
    $walk = function ($folder) use (&$files, &$walk) {
            $list = new FolderItemList();
            $list->filterByParentFolder($folder);
            $list->sortByNodeName();
            $nodes = $list->getResults();

            foreach ($nodes as $node) {
                if ($node->getTreeNodeTypeHandle() === 'file'){
                    $files[] = $node->getTreeNodeFileObject();
                } elseif ($node->getTreeNodeTypeHandle() === 'file_folder'){
                    $walk($node);
                }
            }
        };
    $walk($folder);

    // we are done going through all the folders, we now have our file nodes
    foreach ($files as $file) {
        echo sprintf('%sfile name is %s and URL is %s%s', '<p>', $file->getTitle(), $file->getURL(), '</p>');
    }
}