file->readBytes

Reads bytes from a file.

Target member

Signature

1file->readBytes(count::integer= ?)

Reads bytes from a file.

file->readBytes is called with member syntax on a receiver value. file->readBytes touches paths or filesystem data. Resolve paths deliberately and avoid mixing visitor-provided path fragments with trusted server paths without validation.

Parameters

count
Optional value of type integer. Supply count positionally unless the signature shows a keyword form.

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')->readBytes
The example assumes a trusted path. Validate or resolve any path built from visitor input.

Read a text file safely

1[2local(path) = '/var/www/html/content/about.html'3local(page) = file(#path)45if(#page->exists) => {6    #page->doWithClose => {7        #1->readString->encodeHtml8    }9}10]
`doWithClose` is a useful pattern when the surrounding code should not keep a file handle open.

Related