Trongate Docs
switch to dark modeswitch to dark mode
Building An Uploader Form

Building An Uploader Form

Building a file uploader form with Trongate starts at the controller level.  Our opening salvo is to build a method that will result in our form being drawn on the page.  For example:

function upload() {
    $data['form_location'] = str_replace('/upload', '/submit_upload', current_url());
    $data['view_file'] = 'upload';
    $this->template('admin', $data);

Just To Let You Know
In OOP, a 'method' is the name given to a function that exists inside a class.

Understanding The Upload Method

The first key observation to be made is that there's no particular reason for calling our method 'upload'.  We could have given our method any suitable function name.  In any event, the assumption here is that our uploader form would appear on a page that has:

  • the name of the target module in the first URL segment
  • the name of the uploader method (i.e., 'upload') in the second segment
  • potentially, a database record id as a third URL segment

So, if we were working on a site on localhost named 'demo' and building an uploader for a module named 'users', then the URL for displaying our uploader form might be:


Just To Let You Know
The third segment of the URL shown above (in this case, it's the number 88) represents a hypothetical database record 'id' that corresponds to a 'users' record.


It would be a major security risk, to build an uploader and leave it wide open so that anyone could upload.  So, our method opens with a little security.  In this instance, we're making sure that only users with a role of 'admin' can access our uploader form.

​    $this->module('trongate_security');
​    $this->trongate_security->_make_sure_allowed();​


Since we're going to be building a file uploader and since file uploaders are basically forms, it makes sense to establish a form location.  In truth, we could have declared our form location inside a view file and that would have been fine.  However, for the purposes of providing a clear separation of concerns, we're declaring our form location inside the controller file.  Our form location is immediately made part of a $data array so that it can be passed into a view file.

$data['form_location'] = str_replace('/upload', '/submit_upload', current_url());

As you can see, our form location is established by performing a string replace on the current URL.  So, if our form was displayed on:


...then our form location (the destination that our form posts to) would be:



Finally, we finish our method off by declaring a view file and loading a page.  Here we're loading a page from the 'admin' template.

​    $data['view_file'] = 'upload';
​    $this->template('admin', $data);​


Now, we have to create a view file that contains our uploader form.  The syntax for building a file uploader is remarkably similar to an ordinary HTML form.  In Trongate, our view file code could be as follows:

<h1>Upload File</h1>
<p>Choose a file and then hit 'Upload'.</p>
echo form_open_upload($form_location);
echo form_file_select('text_file');
echo form_submit('submit', 'Upload');
echo form_close();

As you can see, there are only two deviations here from what we might expect to see with a normal HTML form that has been created with Trongate.  The first being that we are using form_open_upload() to produce a form opening tag.  The second key difference is that we're drawing a file uploading field with:

echo form_file_select('my_file');

Just To Let You Know
The 'my_file' argument that gets passed into the form_file_select method gives our form field a name of 'my_file'.  You could, of course, use another name if you wanted to.

Below is a screenshot, showing our uploader form within the context of an 'admin' template:

an example of a file uploader form


If you have a question or a comment relating to anything you've see here, please goto the Help Bar.