{"record":{"id":"2d80bd5b1d7980bb","repo":"sebastianbergmann/php-text-template","slug":"writing-rendered-result-to-s-failed","errorCode":null,"errorMessage":"Writing rendered result to \"%s\" failed","messagePattern":"Writing rendered result to \"(.+?)\" failed","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/Template.php","lineNumber":88,"sourceCode":"\n    public function render(): string\n    {\n        $keys = [];\n\n        foreach (array_keys($this->values) as $key) {\n            $keys[] = $this->openDelimiter . $key . $this->closeDelimiter;\n        }\n\n        return str_replace($keys, $this->values, $this->template);\n    }\n\n    /**\n     * @codeCoverageIgnore\n     */\n    public function renderTo(string $target): void\n    {\n        if (@file_put_contents($target, $this->render()) === false) {\n            throw new RuntimeException(\n                sprintf(\n                    'Writing rendered result to \"%s\" failed',\n                    $target,\n                ),\n            );\n        }\n    }\n\n    /**\n     * @param non-empty-string $file\n     *\n     * @throws InvalidArgumentException\n     *\n     * @return non-empty-string\n     */\n    private function loadTemplateFile(string $file): string\n    {\n        if (is_file($file)) {","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/sebastianbergmann/php-text-template/blob/1e6083ad3a7992a7b5eb60856a078f9d5ce6f714/src/Template.php#L70-L106","documentation":"renderTo() renders the template with the currently set variables and writes the result to $target via file_put_contents(). If that call returns false — the OS refused or failed the write — the library wraps it in a RuntimeException with the target path in the message. The @ suppression is applied so the underlying PHP warning does not double-report; this exception is the only signal you get.","triggerScenarios":"Calling $template->renderTo('/path/to/out') where the target path is in a non-writable directory, the file exists but is not writable, an intermediate directory does not exist, the path is a directory, the disk is full, or SELinux/open_basedir blocks the write.","commonSituations":"Code-generation or report-export scripts run by a deploy user lacking write permission on the output directory; CI containers with read-only filesystems; a hardcoded relative output path resolved against an unexpected CWD; missing parent directory after a config change; full /tmp or disk quota exceeded.","solutions":["Verify the target directory exists and is writable by the PHP process user (is_dir + is_writable), creating it with mkdir($dir, 0777, true) if needed","Check that the target itself is not an existing directory and, if a file, is writable","Check filesystem-level causes: disk full (df -h), read-only mount, quota, SELinux/AppArmor, or open_basedir restrictions","As a workaround, render() to a string and write it yourself so you control error handling and can fall back to another location"],"exampleFix":"// before\n$template->renderTo('/var/lib/app/output/result.php');\n\n// after\n$dir = '/var/lib/app/output/result.php';\nif (!is_dir(dirname($dir))) {\n    mkdir(dirname($dir), 0777, true);\n}\nif (!is_writable(dirname($dir))) {\n    throw new RuntimeException('Output directory not writable: ' . dirname($dir));\n}\n$template->renderTo($dir);","handlingStrategy":"try-catch","validationCode":"if (!is_dir(dirname($target)) || !is_writable(dirname($target)) || (file_exists($target) && !is_writable($target))) {\n    throw new RuntimeException('Cannot write to ' . $target);\n}","typeGuard":null,"tryCatchPattern":"try {\n    $template->renderTo($target);\n} catch (\\RuntimeException $e) {\n    error_log($e->getMessage());\n    // fall back to an alternate output location or abort\n}","preventionTips":["Ensure the output directory exists with correct ownership/permissions before calling renderTo","Use absolute paths instead of CWD-dependent relative paths","Monitor disk space in deployment environments","Remember renderTo swallows the PHP warning (@); always treat it as fallible and catch the RuntimeException"],"tags":["php","filesystem","file-write","io"],"backgroundTag":"file-write-failed","analyzedSha":"1e6083ad3a7992a7b5eb60856a078f9d5ce6f714","analyzedAt":"2026-09-14T11:21:17.064Z","contentChangedAt":"2026-09-14T11:21:17.064Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}