{file}

Writes its rendered content to a file under DATA/ instead of to the page.

Files Tag pairs

Syntax

{file dir='exports', name='orders', ext='csv'} ... {/file}
{file dir='reports', name='daily', ext='html', date, stamp, id=8} ... {/file}

Options

dir
The directory below DATA/; none writes in DATA/ itself.
name
The file name, file by default.
ext
The extension, ext by default.
date
Bare option: _Y-m-d of today added to the name.
stamp
Bare option: a time stamp added to the name.
id
A random suffix of this many characters added to the name.

Description

{file} renders its content as usual - fields, loops, tags - and writes the result to a file instead of to the page; the tag itself shows nothing. The file goes under DATA/: dir= is the directory below it, name= the name (file by default) and ext= the extension (ext by default). An existing file of that name is overwritten.

date adds today's date (_2026-10-08) to the name, stamp a time stamp, and id=8 a random suffix of eight characters, so one run does not overwrite the last.

No name the web server would run is written - .php, .phtml, .phar ... anywhere in it - nor one it reads as configuration, any part starting with a dot such as .htaccess: that is a PAD error, and nothing is written. The tag writes files, so its examples are shown, not run.

Examples

Content to a file

The content renders as usual and goes to DATA/exports/orders.csv instead of the page.

Templateexamples/file/1-write.src
{file dir='exports', name='orders', ext='csv'}
number,customer,total
{orders}{$number},{$customer},{$total}
{/orders}
{/file}
<p>The export is written.</p>

A new name every time

date, stamp and id add the date, a time stamp or a unique id to the name, so one run does not overwrite the last.

Templateexamples/file/2-stamped.src
{file dir='reports', name='daily', ext='html', date, stamp}
  <h1>Report</h1>
  {sales}<p>{$region}: {$amount}</p>{/sales}
{/file}

Names that are refused

A name the web server would run or read as configuration is a PAD error, and nothing is written.

Templateexamples/file/3-refused.src
{file dir='uploads', name='shell', ext='php'}...{/file}        {# .php - refused #}
{file dir='uploads', name='.htaccess', ext='txt'}...{/file}    {# a part starting with a dot - refused #}

See also

  • {files} Scans a directory and iterates its entries, with path, file, ext, item and dir for each.
  • {exists} Renders its content when a file or directory is there, its else half when not.
  • {output} Switches how the running request answers - a web page, the console, a file, a download, JSON or CSV.