Introduction
Basic Concepts
Understanding Routing
Controllers
Views
Assets
Modules Calling Modules
Parent & Child Modules
Database Operations
Modules within Modules
Templates & Themes
Helpers Explained
Form Handling
Working with Files
The Module Import Wizard
Authorization & Authentication
The API Explorer
If you’ve found an error, spotted something missing, or feel a section could be clearer or better explained, we’d love to hear from you. Your feedback helps keep the documentation accurate and useful for everyone.
Please report issues or suggest improvements on GitHub. Community input is invaluable in making the docs stronger.
Not comfortable with GitHub? No problem — you can also get in touch with us directly via our contact form. We welcome all feedback.
File Uploader Example
Let's walk through building a document uploader using Trongate's upload_file() method. This example will demonstrate creating a secure PDF document uploader with proper validation and error handling.
The following example pertains to a hypothetical custom module named as 'documents'.
Step 1: Create the Upload Form
Create a view file (e.g., upload_form.php
) in your module's views directory:
The 'accept' property is an HTML attribute for specifying acceptable file types for uploads. Here's how you can use it:
Step 2: Create the Controller
Create a controller (e.g., documents.php
) in your module's controllers directory:
Step 3: Create the Success View
Create a success view file (e.g., upload_success.php
):
Directory Structure
Your module structure should look like this:
Understanding the Configuration Options
Option | Description | Default |
---|---|---|
destination |
Directory where files will be uploaded | Required |
upload_to_module |
If true, files are uploaded to the module's assets directory | false |
make_rand_name |
If true, generates a random filename for uploaded files | false |
File Upload Validation Rules
Trongate provides several validation rules specifically for file uploads:
required
: Ensures a file was selectedallowed_types[type1,type2]
: Specify allowed file extensions (e.g., pdf,doc,txt)max_size[size_in_kb]
: Maximum file size in kilobytes
- Always validate file types to prevent unauthorized file uploads
- Use
make_rand_name
to prevent filename conflicts and increase security - Set appropriate file size limits to prevent server overload
- Ensure your upload directory has correct permissions (typically 755)
Handling the Upload Response
On successful upload, upload_file() returns an array containing:
file_name
: The name of the uploaded filefile_path
: The complete server path to the uploaded filefile_type
: The MIME type of the uploaded filefile_size
: The size of the uploaded file in bytes
In the next section, we'll explore using the upload_picture() method, which provides additional features specifically for handling image uploads.