Trongate PHP Framework Docs
Introduction
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
Authorization & Authentication
Tips And Best Practices

Basic File Uploading

Let's build a file uploader. Not a theoretical example. Not a "hello world" toy. A real uploader that lets you upload files, using Trongate's File module.

We'll use the following three-method pattern:

  1. Display the upload form
  2. Process the upload with validation
  3. Show the success page

Each method does one thing. Clean. Simple. Maintainable.

In a hurry? Head over to GitHub and download the Simple Uploader module:

https://github.com/trongate/Trongate-v2---Simple-Uploader

The code demonstrates basic file uploading concepts in the clearest possible way.

Uploading images? Image uploads usually require additional processing.

For image uploads, use the Image Module, which includes support for common image manipulation tasks.

The Complete Controller

Create modules/simple_uploader/Simple_uploader.php:

That's the entire controller. Three methods. Zero complexity. Production-ready pattern.

The File module automatically processes the first file uploaded in your form, regardless of its input field name. This makes it consistent with the Image module.

Important: File Input Name

The name attribute of your file input field must match what you're validating. In this example, we use userfile consistently throughout:

  • Form: form_file_select('userfile', ...)
  • Validation: set_rules('userfile', 'File', ...)

The Upload Form View

Create modules/simple_uploader/views/upload_form.php:

About the CSS file: The two view files mentioned on this page reference simple_uploader.css, which should be located at:

modules/simple_uploader/css/simple_uploader.css

Trongate's Module Asset Manager will automatically find and load this file when the views are rendered.

As a reminder, you can download the complete module (including the CSS file) from GitHub:

Simple Uploader Module

The Success Page View

Create modules/simple_uploader/views/upload_success.php:

Create the Upload Directory

One last step - create the directory where files will be stored:

Or create it via PHP if you prefer:

How It Works (The Flow)

  1. User visits yoursite.com/simple_uploader (or yoursite.com/simple_uploader/index)
  2. Form displays with file selection input
  3. User selects file and clicks "Upload File"
  4. Browser POSTs to yoursite.com/simple_uploader/submit_upload
  5. Trongate validates the file (required, type, size)
  6. If valid: Uploads file, sets success message, redirects to success page
  7. If invalid: Redisplays form with error messages
  8. Success page shows filename and "View File" button

Key Concepts Explained

1. form_open_upload() vs form_open()

Use for file uploads. It automatically adds:

That enctype attribute is required for file uploads. Trongate adds it automatically.

2. The POST-Redirect-GET Pattern

Notice, we don't render the success page directly. Instead, when an upload is successful, we immediately direct the user elsewhere:

POST (upload) → ProcessRedirectGET (success page)

3. Validation Before Upload

In addition, we always validate before touching the file system:

Try It Right Now

  1. Create the three files above
  2. Create the uploads directory
  3. Visit: yoursite.com/simple_uploader
  4. Upload a text file (.txt, .md, .csv, or .log)
  5. See the success message and view the file

Don't forget: You're welcome to download the code from GitHub.