Codeigniter has a number of libraries and helpers which you can use for making your code reliable and bugs free. CI provides an extensive library of common usage classes. Upload class in Codeigniter provides us a simple and easy way for uploading files on server.
For uploading a file using codeigniter create a simple HTML file which should contains a file input control and submit button with a form.
Here is the code:
1 2 3 |
echo form_open_multipart('upload/do_upload'); <input name="myFile" size="40" type="file" /> <input type="submit" value="Upload" /> |
REMEMBER : Use form_open_multipart() if you are using any uploading in the form.
Your controller will hold a simple function which will set the config array for defining the uploading path, allowed types, max size and height.
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 |
function do_upload() { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '100'; $config['max_width'] = '1024'; $config['max_height'] = '768'; // You can give video formats if you want to upload any video file. $this->load->library('upload', $config); if ( ! $this->upload->do_upload()) { $error = array('error' = $this->upload->display_errors()); // uploading failed. $error will holds the errors. } else { $data = array('upload_data' => $this->upload->data()); // uploading successfull, now do your further actions } } |
If files that are being uploaded are big in size then you need to increase Max file uploading size in php.ini on your server.
Thank you for your post i want to know how to insert a image name
/*— Controller —*/
$data = array(‘upload_data’ => $this->upload->data());
$this->load->view(‘your-upload-page’, $data);
/*— View –*/
echo $upload_data[‘file_name’]; //this will output the image name
echo $upload_data[‘image_size_str’]; //this will output the width and the height of the image
<img src="” />