Created
November 8, 2017 15:57
-
-
Save bwente/65e4d2fe788727e38e312f4ae2f35a1c to your computer and use it in GitHub Desktop.
MODX snippet to write to a text file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage.
[[!Notepad? &content=`[[#312.content]]` &filename=`product-feed.xml` &filedir=`XML` &result=`success`]]