Seach

file upload in codeigniter

fileupload.php (controller)


<?php
defined('BASEPATH') OR EXIT('Dirrect Access Not Allowed');
class Fileupload extends CI_Controller
{
    public function __construct() {
        parent::__construct();
      
    }
    public function index()
    {
        $this->load->view('fileupload_view',array('error'=>''));
      
    }
  
    public function upload()
    {
        $config['allowed_types']='jpg|pdf|png';//setting allowed extensions
        $config['upload_path']='./assets/'; //path
        $this->load->library('upload',$config); //loading library upload and set upload settings
       
        if($this->upload->do_upload('upload'))
        {
           $data=  array('result'=>$this->upload->data());
           $this->load->view('fileupload_result_view',$data);
        }
 else {
            $this->load->view('fileupload_view',array('error'=>$this->upload->display_errors()));
 }
    }
}
?>
fileupload_view.php(view)


<form method="post" enctype="multipart/form-data" action="<?php echo site_url('fileupload/upload');?>">
    <table border="1">
        <tr>
            <td>
                <label>File Name</label>
            </td>
            <td>
                <input type="file" name="upload">
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" name="save" value="Save">
            </td>
        </tr>
    </table>
</form>
<?php echo $error;?>


fileupload_result_view.php(view)


<?php
foreach ($result as $key=>$data)
{
    echo "<br/>".$key.="".$data;
}
?>