我有一个这样的文件夹结构:
folder1
subfolder1
file1.pdf
file2.pdf
subfolder2
file3.pdf
file4.pdf
有什么方法可以使用“folder1” id 检索所有 pdf 文件(以编程方式)?
我有一个这样的文件夹结构:
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>');
}
}