Learn how to upload files with CodeIgniter

CodeIgniter comes with very good file uploading support, which can take a lot of the hassle out of writing upload functions.

Getting ready

There are some settings you should be aware of which you’ll probably need to change for your environment. Firstly, ensure that you load the upload library using:

$this->load->library('upload');

The following is a table of settings that should be placed in the $config array in the controller you are using, such as the following Fileupload controller:

SettingDefaultChange toDescription
upload_pathNoneNoneIt specifies the path to the folder where the uploaded file should go. Ensure that you have set the correct permissions to enable CodeIgniter to write to it.
allowed_typesNoneNoneIt specifies the allowed mime types, which are allowed in the upload. This can be useful as it allows you to white list uploaded file types; that is, it allows you to define only allowed types. You should separate each type with a pipe (|). For example:
$config['allowed_types'] ="jpg|gif|bmp|png";
file_nameNoneDesired file nameIf this value is set, CodeIgniter will attempt to rename the file to this value on upload.
overwriteFALSETRUE/FALSECodeIgniter looks in the upload destination folder to see if a file with a matching file name already exists. If this is set to TRUE that file will be overwritten; if it is set to FALSE, a number will be appended to the file name.
max_size0NoneIt specifies the maximum size in kilobytes that the file is allowed to be. Setting it to zero will tell CodeIgniter that there is no limit.
max_width0NoneIt specifies the maximum width in pixels. Setting it to zero will tell CodeIgniter that there is no limit.
max_height0NoneIt specifies the maximum height in pixels. Setting it to zero will tell CodeIgniter that there is no limit.
max_filename0NoneIt specifies the maximum character file name length of the uploaded file. Setting it to zero will tell CodeIgniter that there is no limit.
encrypt_nameFALSETRUE/FALSEIt tells CodeIgniter that you wish to encrypt the file name of the image on upload.
remove_spacesTRUETRUE/FALSEIt specifies if you want whitespace removed from the filename on upload.

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

  • its totally irritating … make it as single page post don’t split it

    • Deven Rathore

      ha ha ! will try from next time

Pin It on Pinterest

Shares