The Upload Form
The upload form follows the same admin pattern as the create() method from the Basic CRUD chapter, but with an important difference: it uses form_open_upload() instead of form_open(). This is required because file uploads need a multipart/form-data encoding type.
The Controller Method
The create() method is minimal - it sets up the view data and renders the admin template:
public function create(): void {
$this->gatekeeper();
$data['view_module'] = 'file_manager';
$data['view_file'] = 'create';
$this->templates->admin($data);
}The View File
The view contains a form with two fields: a text input for the document name and a file input for selecting the file.
<div class="container">
<h1>Upload File</h1>
<?= form_open_upload(
'file_manager/submit'
) ?>
<div class="form-group">
<label for="doc_name">
Document Name:
</label>
<?= form_input('doc_name', '', [
'class' => 'form-control'
]) ?>
<?= form_error('doc_name') ?>
</div>
<div class="form-group">
<label for="userfile">
Select File:
</label>
<?= form_file_select('userfile', [
'class' => 'form-control'
]) ?>
<?= form_error('userfile') ?>
<p class="help-block">
Allowed types: PDF, TXT, CSV, DOC, DOCX,
XLS, XLSX, ZIP. Maximum size: 10 MB.
</p>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">
<i class="fa-solid fa-upload"></i>
Upload File
</button>
<a href="<?= BASE_URL ?>file_manager/manage"
class="btn btn-default">
Cancel
</a>
</div>
<?= form_close() ?>
</div>Key Differences From a Regular Form
-
form_open_upload()- Automatically adds theenctype="multipart/form-data"attribute, which is required for file uploads. Usingform_open()instead would silently drop the file data. -
form_file_select()- Renders an<input type="file">element. Thenameattribute (userfile) must match the validation rule name in the controller. -
Validation feedback -
form_error('userfile')displays validation messages next to the file input, just like any other field.
File Input Name Consistency
In the view, the file input is named userfile. In the controller's submit() method, the validation rule references the same name:
$this->validation->set_rules(
'userfile', 'File',
'allowed_types[pdf,txt,csv,doc,docx,xls,xlsx,zip]'
. '|max_size[10240]'
);
The File module automatically processes the first file uploaded in your form, regardless of its input field name. However, for validation to work correctly, the name attribute of the file input must match the validation rule. Using userfile consistently throughout is a reliable convention.
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.