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.

 
mnakalay replied on at Permalink Reply
mnakalay
Concerning the creation of a folder, this is how you can do it.
use Concrete\Core\File\Filesystem;
$filesystem = new Filesystem();
$folder = $filesystem->getRootFolder();
$folderName = t("Your folder's name");
$folder = $filesystem->addFolder($folder, $folderName);

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
use Concrete\Core\File\Filesystem;
use Concrete\Core\Tree\Node\Node;
use Concrete\Core\Tree\Node\Type\FileFolder;
$filesystem = new Filesystem();
$folderName = t("Your folder's name");
$folder = Node::getByID($yourExistingFolderID);
// alternativaly if you don't have your existing folder's ID you can use this to grab it by name
// $folder = Node::getNodeByName($yourExistingFolderName);
if (is_object($folder) && $folder instanceof FileFolder) {
    $folder = $filesystem->addFolder($folder, $folderName);
}
mnakalay replied on at Permalink Best Answer Reply
mnakalay
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);
}
linuxoid replied on at Permalink Reply
linuxoid
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);
    }
mnakalay replied on at Permalink Reply
mnakalay
Yes you are right, getNodeByName() filters the result by node type
mnakalay replied on at Permalink Reply
mnakalay
Please keep in mind none of this actually checks for permissions