How to upload image file in Codeigniter How to upload image file in Codeigniter codeigniter codeigniter

How to upload image file in Codeigniter


Try this hope it will help you

$config['upload_path'] = './asset/upload/';$config['allowed_types'] = 'gif|jpg|png';$config['max_size']     = 2048000;$config['max_width'] = 1024;$config['max_height'] = 1024;$config['file_name']        = $this->input->post('photo');$this->load->library('upload', $config);if (!$this->upload->do_upload($config['file_name'])) {    $error = $this->upload->display_errors();    //coding...}else {    $filename = $this->upload->data();    $data[$config['file_name']] = $filename['file_name'];    //coding...}


You should catch the file by

$config['file_name'] = $_FILES['name']['photo'];

Than you will be able to get the image file to process.

Before it please check if the field is able to get or not by

if(!empty($_FILES['name']['photo']))

This was perfectly working on my computer. Hope it will be helpful for you.


You don't need to do this: $config['file_name'] = $this->input->post('photo');

By default the upload class will use the name of the image as the name of the final image. If you wanted to do it this way than $_FILES['photo']['name']; would work (but again, there is no need).

Further you aren't getting the value of the uploaded photo correctly. I'm not sure why you are putting the config array in this function: $this->upload->data($config); but that is not how it supposed to be used.

If you wanted to get the filename for example you could do:

$filename = $this->upload->data('file_name');

OR

$up_data = $this->upload->data();$filename = $up_data['file_name']

See this for reference: https://www.codeigniter.com/userguide3/libraries/file_uploading.html#CI_Upload::data


Also you are not doing anything with this line: $error = array ('error' =>$this->upload->display_errors());. I suggest setting the error as a flash message before redirecting.