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:

Setting Default Change to Description
upload_path None None It 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_types None None It 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_name None Desired file name If this value is set, CodeIgniter will attempt to rename the file to this value on upload.
overwrite FALSE TRUE/FALSE CodeIgniter 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_size 0 None It 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_width 0 None It specifies the maximum width in pixels. Setting it to zero will tell CodeIgniter that there is no limit.
max_height 0 None It specifies the maximum height in pixels. Setting it to zero will tell CodeIgniter that there is no limit.
max_filename 0 None It specifies the maximum character file name length of the uploaded file. Setting it to zero will tell CodeIgniter that there is no limit.
encrypt_name FALSE TRUE/FALSE It tells CodeIgniter that you wish to encrypt the file name of the image on upload.
remove_spaces TRUE TRUE/FALSE It 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.

Pin It on Pinterest

Shares