The File Module

The File Module provides a robust set of tools for managing file operations within the Trongate framework. It supports essential functionalities such as secure file uploading with automatic MIME validation, directory creation, reading and writing file contents, and file transfers (downloads).

Security is a core feature of this module, with built-in protections against directory traversal attacks and restricted access to sensitive application directories like "config" and "engine". It also includes utilities for managing memory requirements and ensuring unique file naming during uploads.

Comprehensive guidance for this module can be found in the File Management chapter of the Trongate PHP framework documentation.

copy()

Copies a file from one location to another. Validates access to the source path before copying.

create_directory()

Creates a new directory at the specified path. This method allows for the creation of nested directories if they do not exist, subject to application security restrictions.

delete()

Deletes a file from the filesystem. Throws an exception if the file does not exist, cannot be deleted, or is in a restricted path.

download()

Initiates a file download or displays a file inline. This method handles header preparation, including Content-Type and Content-Disposition.

exists()

Checks whether a file or directory exists at the specified path.

info()

Retrieves metadata about a file, including its size, last modification time, permissions, and MIME type.

list_directory()

Lists all files and directories within a specified path. Returns a simple array of names for flat listings, or a nested array structure when using recursive mode to traverse subdirectories.

move()

Moves (renames) a file from one location to another. Validates access to the source path before moving.

read()

Reads and returns the contents of a file. Access is restricted for critical application directories.

upload()

Handles the file upload process with specified configuration. This method validates the configuration, processes the uploaded file, performs security checks, generates a secure filename, and moves the file to the target destination.

write()

Writes or appends data to a file. This method includes path validation to prevent writing to restricted application areas.