by Bhumi // Mar 19,2013 // No comment
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.If you want to upload image in codeigniter,you need to write below html code into your view file which will create field to upload image.
|
1 2 3 4 5 6 7 8 9 10 11 |
<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 |
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')) { $error = array('error' => $this->upload->display_errors()); 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 folder.
Thank you for reading, good luck and let me know if you run in to 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.
CodeIgniter is great framework by its simplicity because structure of the CodeIgniter framework is clear and flexible. Let’s understand the function used to get user’s READ MORE
Pagination is a number of rows to be fetched from the database and mostly found at the bottom of the page which allow you to READ MORE
Today I am going to write a short post about the _remap() function in CodeIgniter which is used to remap the function with Controller. In READ MORE
Prepping Functions in CodeIgniter
We’ve looked basics about CodeIgniter at here which is about installation steps and basics about CodeIgniter. Today I am going to cover prepping functions used READ MORE
Getting Started with CodeIgniter
Today i am going to cover small tutorial about Codeigniter.CodeIgniter is a PHP framework for web application.Basically CodeIgniter follows the most well known Model, view READ MORE
Be a Contributor at CreativeDev.Write an article or tutorial to the community and share some of your knowledge!