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:
- Display the upload form
- Process the upload with validation
- 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)
- User visits
yoursite.com/simple_uploader(oryoursite.com/simple_uploader/index) - Form displays with file selection input
- User selects file and clicks "Upload File"
- Browser POSTs to
yoursite.com/simple_uploader/submit_upload - Trongate validates the file (required, type, size)
- If valid: Uploads file, sets success message, redirects to success page
- If invalid: Redisplays form with error messages
- 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) → Process → Redirect → GET (success page)
3. Validation Before Upload
In addition, we always validate before touching the file system:
Try It Right Now
- Create the three files above
- Create the uploads directory
- Visit:
yoursite.com/simple_uploader - Upload a text file (.txt, .md, .csv, or .log)
- See the success message and view the file
Don't forget: You're welcome to download the code from GitHub.
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.