Create and moving files to the folder programmatically
Permalink
Can someone help me on how I can create a folder in FileManager and move a file into that created folder programmatically?
As I have learned so far, the new folder feature of Concrete5 has been made on the top of 'Topics'. But the documentation on this feature is not available on the 'File' section.
As I have learned so far, the new folder feature of Concrete5 has been made on the top of 'Topics'. But the documentation on this feature is not available on the 'File' section.
To move a file or files to the folder you'd do this
use Concrete\Core\Tree\Node\Node; use Concrete\Core\File\File $destFolder = Node::getByID($yourFolderID); // alternatively use the same code as above to grab the folder by name instead // You should have your file object already, let's call it $file $fileNode = $file->getFileNodeObject(); if (is_object($fileNode)) { $fileNode->move($destFolder); }
Slight change, the line with with Node must be FileFolder:
$img_url = $pkg->getPackagePath() . '/img/sample/test.png'; $file = $importer->import($img_url); if ($file instanceof Version) { $set->addFileToSet($file); $folder = $file_system->getRootFolder(); $folder_name = t('Test Folder'); $folder = $file_system->addFolder($folder, $folder_name); $folder = FileFolder::getNodeByName('Test Folder'); // <- *************** $file_node = $file->getFile()->getFileNodeObject(); if (is_object($file_node)) { $file_node->move($folder); }
Yes you are right, getNodeByName() filters the result by node type
Please keep in mind none of this actually checks for permissions
This only adds a folder to the root of the file manager.
If you want to add a folder inside an existing folder do this