Codeigniter File Upload - Web Development and Web Design Codes

Latest

Friday, February 16, 2018

Codeigniter File Upload

Codeigniter File Upload

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:
codeigniter file upload

Configuration:
Auto loading URL Helper application/config/autoload.php
$autoload['helper'] = array('url');

Base_URL Configuration application/config/config.php
$config['base_url'] = 'http://localhost/codeigniter/';

Controllers: blog.php
application/controllers/blog.php
<?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..
<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>
Views: success.php
To display the uploaded file and information create success.php file and copy the below codes..
<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
Codeigniter File Upload
That's it Friend How to upload file in Codeigniter..Thank You Friends..Keep Visiting This Blog for More Codes...

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.