Skip to content

Instantly share code, notes, and snippets.

@bwente
Created November 8, 2017 15:57
Show Gist options
  • Save bwente/65e4d2fe788727e38e312f4ae2f35a1c to your computer and use it in GitHub Desktop.
Save bwente/65e4d2fe788727e38e312f4ae2f35a1c to your computer and use it in GitHub Desktop.
MODX snippet to write to a text file.
<?php
$content = $modx->getOption('content', $scriptProperties, 'eof');
$filename = $modx->getOption('filename', $scriptProperties, 'text.txt');
$filedir = $modx->getOption('filedir', $scriptProperties, 'assets');
$append = $modx->getOption('append', $scriptProperties, false);
$fopen = "w";
if ($append > 0){
$fopen = "a";
};
$fileURL = (string) ($modx->config['base_path']. $filedir .'/'. $filename);
// Let's make sure the file exists and is writable first.
if (is_writable($fileURL)) {
if (!$handle = fopen($fileURL, $fopen)) {
echo "Cannot open file ($fileURL)";
exit;
}
// Write $content to our opened file.
if (fwrite($handle, $content . "\r\n") === FALSE) {
echo "Cannot write to file ($fileURL)";
$modx->log(modX::LOG_LEVEL_ERROR,'Cannot write to file '.$fileURL);
exit;
}
// echo "Success, wrote ($content) to file ($fileURL)";
fclose($handle);
} else {
$modx->log(modX::LOG_LEVEL_ERROR,'The file '. $fileURL . ' is not writable');
}
return true;
@bwente
Copy link
Author

bwente commented May 26, 2023

Example usage.

[[!Notepad? &content=`[[#312.content]]` &filename=`product-feed.xml` &filedir=`XML` &result=`success`]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment