Fork me on GitHub

Nibble Development

Nibble Development

PHP classes and libraries for developers

File upload field

The file upload field allows the user to upload a file to the server. The file can be of many types and can also be as restricted as desired. The only required argument for the file upload field is the label, however there are five additional, optional arguments to further customise the field. The most simple instantiation of the “File” PHP class should look like this:

Type: The second argument supplies the PHP class with the list of mime types to allow to be uploaded. This can be a specific string or an array. The strings are:

  • image: Allows only JPG, PNG and GIF files to be uploaded
  • document: Allows only documents such as Word/Open Office file to be uploaded
  • archive: Allows only archives such as TAR, ZIP and GZ files to be uploaded
  • all: Allows all of the above file types to be uploaded

The value can also be an array, the array must contain strings of allowed mime types, these will then be the only mime types allowed. This allows for full control over the mime types that the user can upload:

Required: The third argument is required, True by default, this is the same as all other fields “required” argument.

Max size: The fourth argument is the maximum file size allowed. This value is an integer which represents the file size in bytes, therefore a 1kb file has the max size of 1024.

Width/height: The fifth and sixth arguments apply only to images and represents the maximum width and height respectively of an image in pixels. This means that the user can only upload an image upto the specified dimensions

Minimum width/height: The seventh and eighth arguments apply only to images and represents the minimum width and height respectively of an image in pixels. This means that the user can only upload an image larger the specified dimensions. Combined with the width and height arguments this can make very specific restictions on image size

A fully customised file upload field instantiation could look something like this:

Please note: Depending on the security of the application, this validation should not be relied upon for images in terms of making sure they are safe, there are other methods that should be employed when the image is saved to the server. This PDF explains the necessary methods to employ.

Luke Rotherfield

Freelance PHP Developer
is a PHP developer dedicated to writing the most awesome liraries for his fellow devs :) enjoy

Latest posts by Luke Rotherfield (see all)

Leave a Comment