Codeigniter File Upload
![]() |
Codeigniter File Upload |
Hi Friends Today we are going to learn how to upload file using codeigniter..
just follow the below Codeigniter File upload Example..
Codeigniter Files Structure:
Configuration:
Auto loading URL Helper application/config/autoload.php
1 | $autoload [ 'helper' ] = array ( 'url' ); |
Base_URL Configuration application/config/config.php
1 |
Controllers: blog.php
application/controllers/blog.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php class Blog extends CI_Controller{ public function index(){ $this ->load->view( 'index' ); } public function do_upload(){ $config [ 'upload_path' ]= './images/' ; //Your Uploading Path $config [ 'allowed_types' ]= 'gif|jpg|jpeg|bmp|png' ; $config [ 'max_size' ]=2048; //2048=2 MB $config [ 'max_width' ]=0; $config [ 'max_height' ]=0; $this ->load->library( 'upload' , $config ); if (! $this ->upload->do_upload( 'image' )){ $error = array ( 'error' => $this ->upload->display_errors( '<div class=error>' , '</div>' )); $this ->load->view( 'index' , $error ); } else { $data = array ( 'upload_data' => $this ->upload->data()); $this ->load->view( 'success' , $data ); } } } ?> |
Views: index.php
To display the upload form create index.php file and copy the below codes..
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 | < html > < head > < title >Codeigniter File Upload</ title > < style > .error{ font-weight:bold; font-size:18px; color:red; } </ style > </ head > <? php if(isset($error)){ echo $error;}?> < h3 >Codeigniter File Upload codenair.com</ h3 > < form method = "POST" enctype = "multipart/form-data" action="<?php echo base_url().'index.php/blog/do_upload';?>"> < table > < tr > < td >Your File</ td > < td >< input type = "file" name = "image" /></ td > </ tr > < tr > < td ></ td > < td >< input type = "submit" value = "Upload" /></ td > </ tr > </ table > </ form > </ html > |
To display the uploaded file and information create success.php file and copy the below codes..
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 | < html > < head > < title >File Upload Success--codenair.com</ title > < style > body{ background:#ccc; } #info{ float:left; width:200px; height:360px; overflow:scroll; border:1px solid green; border-radius:3px; } .image{ height:360px; width:250px; border-radius:4px; } </ style > </ head > < body > < div id = "info" > < h3 >Your Files Info</ h3 > < ul > <? php foreach ($upload_data as $item => $value):?> < li ><? php echo $item;?>: <? php echo $value;?></ li > <? php endforeach; ?> </ ul > </ div > < div id = "image" > < img class = "image" src="<?php echo base_url();?>/images/<? php echo $upload_data['file_name'];?>"></ img > < p ><? php echo anchor('blog/index', 'Upload Another File!'); ?></ p > </ div > </ body > </ html > |
Now visit http://localhost/codeigniter/index.php/blog/ and start upload.
Once file upload complete you will see success page like below..
![]() |
Codeigniter File Upload |
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.