When I first time try to write code for image upload on Codeigniter, I had to face problems. I dig and dig and dig. I search on CI guide. but, thats not enough to help me. Well, finally I did it. If someone stuck on CI image upload, hope it will help them a lot.
There are some easy steps to upload image on CI :
- Create the form normally how it should create for upload a photo on php. Must add “enctype=”multipart/form-data”
- open controler file. under the upload image function write down following code :
- $config[ 'upload_path' ] = ‘./uploads/’ ; // destination folder path
- $config[ 'allowed_types' ] = ‘gif|jpg|png’ ; // Allowed uploaded file type
- $config[ 'max_size' ] = ‘2048’ ;// Allowed uploaded file’s max size
- $this->load->helper( array( ‘form’ , ‘url’ ) ) ;
- $this->load->library( ‘upload’ , $config);
- $image_up = $this->upload->do_upload(‘image‘);
- $image_data = array(‘upload_data’ => $this->upload->data());
- $image_up is where image upload information will be safe, it will return 1 if image uploaded successfully.
- $config is very important variable. for details of “$config” variable’s parameters you must have to see the manual
- $image_data will carry the uploaded image information.
- BLUE marked words are library function.
- image is form name