File Handling Quick Reference
This reference lists all methods available in Trongate's File module when calling $this->file->method_name().
Note: All file operations include automatic security validation to prevent directory traversal attacks and unauthorized access to restricted areas (config, engine).
File Upload
| Method | Parameters | Returns | Description |
|---|---|---|---|
array $config |
array |
Uploads a file to the specified destination with security validation and optional random naming. |
Configuration Array Keys
| Key | Type | Default | Description |
|---|---|---|---|
destination |
string | required | Target directory path (relative to application root or module). |
upload_to_module |
bool | false |
If true, uploads to the module directory. |
target_module |
string | current segment | Specifies which module to upload to when upload_to_module is true. |
make_rand_name |
bool | false |
If true, generates a secure random 10-character alphanumeric filename. |
Example Usage
File Operations
| Method | Parameters | Returns | Description |
|---|---|---|---|
string $file_path |
bool |
Deletes the specified file. Returns true on success. | |
string $sourcestring $destination |
bool |
Copies a file from source to destination. Returns true on success. | |
string $sourcestring $destination |
bool |
Moves a file from source to destination. Returns true on success. | |
string $file_path |
string |
Reads and returns the entire contents of a file. | |
string $file_pathmixed $databool $append = false |
bool |
Writes content to a file. Set $append to true to add to existing content. |
|
string $file_pathbool $as_attachment = true |
void |
Initiates file download or displays inline. Calls exit after sending file. |
File Information & Directories
| Method | Parameters | Returns | Description |
|---|---|---|---|
string $file_path |
array |
Returns detailed metadata about a file (size, MIME type, permissions, etc.). | |
string $file_path |
bool |
Checks if a file or directory exists at the specified path. | |
string $pathint $perms = 0755bool $recursive = true |
bool |
Creates a directory with specified permissions. Returns true if created or already exists. | |
string $pathbool $recursive = false |
array |
Lists all files/directories within the specified path. Returns nested array if recursive. |
Validation Rules
Use these rules with $this->validation->set_rules() during file uploads.
| Rule | Parameter | Description |
|---|---|---|
allowed_types |
extensions | Comma-separated extensions (e.g., allowed_types[jpg,png]). |
max_size |
KB | Maximum file size in kilobytes (e.g., max_size[2048]). |
max_width / max_height |
Pixels | Maximum image dimensions. |
min_width / min_height |
Pixels | Minimum image dimensions. |
square |
None | Validates that the image has a 1:1 aspect ratio. |
Common Patterns
Pattern 1: Basic Upload with Validation
The simplest complete upload workflow following the three-method pattern.
The Three-Method Pattern: This clean pattern separates concerns - one method displays the form, one processes the upload, and one shows success. When validation fails, we call $this->upload_form() to redisplay the form with errors. When validation succeeds, we redirect to prevent duplicate submissions.
Pattern 2: Temporary File Processing with Cleanup
When you need to process a file temporarily before moving it to permanent storage, use this pattern with automatic cleanup.
Why Use a finally Block: The finally block ensures temporary files are always cleaned up, even if an error occurs during processing. This prevents orphaned temporary files from accumulating on your server. The cleanup code runs whether the upload succeeds or fails.