CodeIgniter makes it easy to add images to your site. You can upload them using code I am going to explain here in this post that How to upload image in CodeIgniter?.If you want to upload an image in CodeIgniter, you need to write following HTML code into your view file which will create the field to upload an image.
1 2 3 4 5 6 7 8 9 10 11 12 | <html> <head> </head> <body> <form name="upload" id="upload" method="post" action="/user/test1" enctype="multipart/form-data"> Select File <input type="file" name="txtImage" size="20" /> <input type="submit" value="submit" /> </form> </body> </html> |
Now in the form action, at the PHP side, your code should be this. Here txtImage is your input field name. You have to provide upload_path, allowed types, max_size etc. You can ecrypt the file name by passing encrypt_name = true. The else part creates image thumbnail for specified height and width.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | if($_FILES['txtImage']['name'] != "") { $config['upload_path'] = 'images/upload/'; $config['allowed_types'] = 'gif|jpg|png|jpeg'; $config['max_size'] = '2000'; $config['remove_spaces'] = true; $config['overwrite'] = false; $config['encrypt_name'] = true; $config['max_width'] = ''; $config['max_height'] = ''; $this->load->library('upload', $config); $this->upload->initialize($config); if (!$this->upload->do_upload('txtImage')) { print_r($error); exit(); } else { $image = $this->upload->data(); if ($image['file_name']) { $data['file1'] = $image['file_name']; } $product_image = $data['file1']; $config['image_library'] = 'gd2'; $config['source_image'] = '/images/upload/'.$data['file1']; $config['new_image'] = '/images/upload/new/'; $config['maintain_ratio'] = FALSE; $config['overwrite'] = false; $config['width'] = 280; $config['height'] = 280; $this->load->library('image_lib', $config); //load library $this->image_lib->clear(); $this->image_lib->initialize($config); $this->image_lib->resize(); //do whatever specified in config echo "all the things go well."; } } |
That’s it, Your image is get uploaded to the folder.you can have a look into the folder. 🙂
Thank you for reading, good luck and let me know if you run into any problems & I’ll help as much as I can 🙂
Hope this post will helpful for you, waiting for your responses.Thanks for reading and feel free to share your thoughts! Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.
Comments (4)