-1

I have a folder structure likes this:

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

is there any way to retrieve all the pdf file's(programmatically) using the "folder1" id?

Midhun
  • 1,107
  • 10
  • 24

1 Answers1

1

There is but make sure you don't go insanely deep with your folder structure, you don't want to go through hundreds of folders, hundreds of levels deep.

Here's the code

<?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>');
    }
}
Nour Akalay
  • 429
  • 3
  • 4
  • 1
    Nice reponse but you have to check that the search folder permission are set to guest. If not this function not work if user is not logged https://domain.name/index.php/dashboard/system/files/permissions. – Hugo Pereira Mar 01 '20 at 21:02