file

Creates a file object.

Target type

Signature

1file(path::string= ?)

Creates a file object.

file constructs or identifies a LaiRu value type. file touches paths or filesystem data. Resolve paths deliberately and avoid mixing visitor-provided path fragments with trusted server paths without validation.

Parameters

path
Optional value of type string. Supply path positionally unless the signature shows a keyword form.

A file value is a handle to a path plus the operations performed through that handle. Create it from a trusted server-side path where possible. When a visitor chooses a file, reduce their input to a narrow relative path or filename, reject absolute paths and parent-directory segments, and only then construct the file object.

In web-serving modes, request-targeted application file access cannot escape the configured document_root. That boundary can come from the .ini file, the --docroot flag used by the LaiRu HTTP server or FPM process, or FastCGI values such as DOCUMENT_ROOT and SCRIPT_FILENAME supplied by nginx or Apache. See the web root directory, FastCGI options, and LaiRu FPM pages for the deployment side of that boundary.

Examples

Basic call

1file('/tmp/example.txt')
The example assumes a trusted path. Validate or resolve any path built from visitor input.

Create a file object from a trusted path

1[2local(page) = file('/var/www/html/index.lasso')3if(#page->exists) => {4    #page->path + ' has ' + #page->size + ' bytes'5}6]
A file object represents a path. Use member methods such as `exists`, `path`, `size`, and `modificationTime` to inspect it.

Validate visitor input before creating a file

1[2local(document_root) = '/var/www/html'3local(requested) = string(web_request->param('path', 'index.lasso'))->trim45if(#requested->beginsWith('/') || #requested->contains('..')) => {6    fail('Bad path')7}89local(target) = file(#document_root + '/' + #requested)10if(#target->exists) => {11    #target->readString->encodeHtml12else13    web_response->setStatus(404, 'Not Found')14}15]
Validate the relative path before constructing a file object from request data. The web root boundary is configured by the serving mode.

Related