Trongate PHP Framework Docs
Introduction
Quick Start
Basic Concepts
Understanding Routing
Intercepting Requests
Module Fundamentals
Database Operations
Templates
Helpers
Form Handling
Form Validation
Working With Files
Image Manipulation
Working With Dates & Times
Language Control
Security
Tips And Best Practices

File Management Beyond Uploads

Uploading files is just the beginning. Once files are on your server, you need to manage them.

Trongate's File module provides methods for all common file operations:

  • Checking if files exist
  • Deleting files
  • Copying files
  • Moving/renaming files
  • Reading file contents
  • Writing to files
  • Getting file information

Before proceeding: This page assumes you understand how file paths work in Trongate. All examples use relative paths for PHP operations.

The Pattern: Check Before You Act

All file management methods follow a simple calling pattern:

The methods introduced on this page throw exceptions for genuine errors (permission denied, disk full, file locked) - not for expected conditions that you should handle in advance.

It's highly recommended to make sure appropriate conditions are met before trying to invoke file management methods.

Make sure files exist before calling file management methods:

1. Checking File Existence

Use to make sure a file exists before operating on it:

Example: Verify Before Deletion

2. Deleting Files

The method removes files from the file system:

Example: Clean Up Old Uploads

3. Copying Files

The method copies a file from one location to another.

Example: Create Document Backup

4. Moving Files

With you can relocate or rename files in a single operation:

Example: Organize Files by Date

5. Reading Files

The method returns complete file contents as a string:

Example: Process CSV Data

6. Writing Files

With the method, you can create new files or update existing ones:

Example: Generate Export File

Example: Append to Log File

7. Getting File Information

The method can be used to retrieve comprehensive metadata about a file:

Returns an array containing:

  • file_name - The filename
  • size - Size in bytes
  • human_readable_size - Size formatted (e.g., "2.4 MB")
  • modified_time - Unix timestamp of last modification
  • permissions - File permissions
  • readable_permissions - Formatted permissions (e.g., "0644")
  • mime_type - MIME type of the file

Example: Display File Details

Quick Reference Table

Method Purpose Example
Check if file exists if ($this->file->exists($path))
Remove a file $this->file->delete($file_path)
Duplicate a file $this->file->copy($source, $destination)
Relocate/rename a file $this->file->move($old_path, $new_path)
Get file contents $content = $this->file->read($file_path)
Create or update file $this->file->write($path, $data)
Get file metadata $info = $this->file->info($file_path)

Common Patterns and Best Practices

Pattern 1: Store Relative Paths in Database

Pattern 2: Store Filename, Reconstruct Path

Pattern 3: Complete Validation Flow

We're continually improving the Trongate documentation. If anything is incorrect, unclear, incomplete, or could be better, we'd genuinely appreciate your input.

Share your thoughts in the Documentation Feedback.

Leave Feedback About This Page