Trongate Docs
switch to dark modeswitch to dark mode
»
»
The File Validation Helper

The File Validation Helper

Trongate is loaded with a variety of features that can assist with the uploading of files such as images.  Before exploring Trongate's file validation helper, let's have a reminder of how to draw a file uploader form using Trongate:

echo form_open_upload('members/submit_upload_picture/'.$update_id);
validation_errors();
echo 'Please choose a picture from your computer and then press \'Upload\'.';
echo form_file_select('picture');
echo form_submit('submit', 'Upload');
echo form_close();

The code above could be used to produce a form that looks like this:

file uploader example

Just To Let You Know
The example shown in the screenshot contains some CSS and a little additional HTML to improve the appearance of the uploader form.

Understanding The Uploader Form

The uploader form, above, starts by invoking the form_open_upload() method.  

echo form_open_upload('members/submit_upload_picture/'.$update_id);

As can see, an argument has been passed into the method.  The argument represents the destination for the uploader form.  The destination is the URL where the user is sent to when the form is submitted.  The opening line produces the following HTML code:

<form action="http://localhost/your_app/members/submit_upload_picture/1" method="post" enctype="multipart/form-data">


Just To Let You Know
The assumption being made with this example, is that an $update_id variable has been passed into the view file.  We're also assuming that the BASE_URL value is set to http://localhost/your_app/

Next, we invoked Trongate's validation_errors() method.  

validation_errors();

The validation errors method displays form validation errors, if there are any.

After displaying some text based instructions, the next important part of our form is:

echo form_file_select('picture');

The form_file_select method displays a form file uploader field.  As you can see, the argument 'picture' has been passed into the method.  The 'picture' argument represents the name of the file uploader form field.  The HTML code that gets generated from our form_file_select method is shown below:

<input type="file" name="picture" value="">

Just To Let You Know
The form file select method can accept the following three arguments:

  • name
  • attributes
  • additional code

What these three attributes represent and how they behave is identical to how ordinary form_input() methods work.  See The Form Helper Documentation for details.

Finally, we end our uploader form by displaying a submit button and by drawing a closing 'form' tag.  Here, our 'submit' button has the name 'submit' but the word 'Upload' appearing inside the button:

echo form_submit('submit', 'Upload');
​echo form_close();

Applying Form Validation Tests To Files

Validation for file uploading scenarios is handled in precisely the same manner as normal form validation.

Below is a complete list of validation tests that are available for file uploading scenarios.

Rule Parameter Description Example
allowed_types Yes Returns FALSE if file is not of a pre-defined type. allowed_types[gif,jpg,jpeg,png]
max_size Yes Returns FALSE if file size is greater than given file size (in kilobytes). max_size[2000]
max_height Yes Returns FALSE if file is an image that exceeds the maximum allowed height (in pixels). max_height[480]
max_width Yes Returns FALSE if file is an image that exceeds the maximum allowed width (in pixels). max_width[640]
min_height Yes Returns FALSE if file is an image with height that falls below minimum allowed height (in pixels). min_height[480]
min_width Yes Returns FALSE if file is an image with width a width that falls below minimum allowed width (in pixels). min_width[640]
square Yes Returns FALSE if file is an image with a width that is not equal to the image height. min_width[640]

 



HELP & SUPPORT

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

 
×