8.4.2: how to create xml file programmatically in concrete5?

Permalink
I can create an xml file with PHP, which can be stored in the application/files folder. But is there a concrete5 way of creating an xml file so that I can store it in a File Manager folder? I don't want the xml to be stored in the files folder, only in the FM.

I can of course create one in the files, then move it into the FM, then delete the original in files. But is there a more concrete5onic way?

linuxoid
 
dimger84 replied on at Permalink Reply
dimger84
Check these links:
https://documentation.concrete5.org/developers/working-with-files-an...

https://documentation.concrete5.org/developers/working-with-files-an...

Also take a look in \Concrete\Core\File\Service\File class.

I think the way to do it is create the file in the temporary folder and then import it to File Manager with the concrete5 importer.
linuxoid replied on at Permalink Reply
linuxoid
That's basically what I'm doing:
$file_system = new Filesystem();
$root_folder = $file_system->getRootFolder();
$folder_name = t('My Folder');
$folder = FileFolder::getNodeByName($folder_name);
if (!is_object($folder) || !$folder instanceof FileFolder) {
    $file_system->addFolder($root_folder, $folder_name);
}
$fh = $this->app->make('helper/file');
$temp_dir = (string)$fh->getTemporaryDirectory();
if ($temp_dir === '' || !is_dir($temp_dir)) {
    throw new UserMessageException(t('Unable to determine temporary directory'));
}
$temp_fname = @tempnam($temp_dir, 'xml_') . '.xml';
$doc = new \DOMDocument('1.0', 'UTF-8');
$doc->formatOutput = true;

An xml file type is added to the FM's allowed extensions. The xml file is created but it doesn't get imported into the FM's folder, the folder's empty. The imported returns a 'Invalid file extension' error. What am I missing?
linuxoid replied on at Permalink Reply
linuxoid
I also tried writing an xml string into a file:
$fd = null;
$fd = fopen($temp_fname, 'wb+');
if (@fwrite($fd, $doc->saveXML()) === false) {
    throw new UserMessageException(t('Failed to write to temporary file'));
}
fflush($fd);
fclose($fd);
$fd = null;
@chmod($temp_fname, 0644);
$importer = new Importer();
$file = $importer->import($temp_fname, 'xml_file', $folder);

still get the same 'Invalid file extension' error.
linuxoid replied on at Permalink Reply
linuxoid
It turned out to be the destination's file extension error, not the source's, thanx to this post:
https://www.concrete5.org/community/forums/customizing_c5/fileimport...
$file = $importer->import($temp_fname, false, $folder);

fixed that.

I read
https://documentation.concrete5.org/api/8.5.2/Concrete/Core/File/Imp...
as "$filename A custom name to give to the file" is the name without extension, but it needs one.