For smart Primates & ROBOTS (oh and ALIENS ).

Blogroll

Sunday, March 29, 2015

File upload in CodeIgniter

Problem:

How to upload file in CodeIgniter?
You can simply use $_FILES array to upload image. But if you want to use CodeIgniter library to upload file, then how can you achieve this? 

Solution:

You can use $this->load->helper('form'); to generate form.
After that configure using $config[], set this $config[] same as $this->load->library('upload', $config);
and upload like this
$this->upload->do_upload('transporter_image');

Error may occur during file upload so you can get error list by using

$his->upload->display_errors());
If file uploaded the get the information by this method:
$this->upload->data();

Example:

VIEW FILE
transporter_form.php
<html>
<head>
<title>File uploading in CodeIgniter</title>
</head>
<body>
<?php echo form_open_multipart('Transporter/transporterlist/');?>
<input type="file" name="transporter_image" size="20" />
<br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
============================================
CONTROLLER FILE
Transporter.php
<?php
class Transporter extends CI_Controller {
        public function __construct()
        {
                parent::__construct();
                $this->load->helper('form');
        }

        public function index()
        {
                $this->load->view('transporter_form', array('error' => ' ' ));
        }

        public function transporterlist ()
        {
        $config['upload_path']          = './images/transporter/';
            $config['allowed_types']        = 'gif|jpg|png';
            $config['max_size']             = 100;

            $config['max_width']            = 1024;
            $config['max_height']           = 768;
        $config['file_name']           = 'file_name.jpg';// set file name here
        $this->load->library('upload', $config);
        if(!$this->upload->do_upload('transporter_image')) {
                    $data['imgerror']= array('error' => $this->upload->display_errors());
        } else {
                    $data['imgsuccess'] = 'Image uploaded';//array('success' => $this->upload->data());
        }
        $this->load->view('successpage',$data);
        }
}
?>
==============================================
VIEW FILE
successpage.php
<html>
<head>
<title></title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<?php
    if(isset($imgsuccess)){
        echo $imgsuccess;
    }
?>

<ul>
<?php
if(isset($imgerror)) {
    foreach ($imgerror as $item => $value):?>
        <li><?php echo $item;?>: <?php echo $value;?></li>
<?php
    endforeach;
}
?>
</ul>
</body>
</html>








Share:

0 comments:

Post a Comment

How to make center align child div

 How to make center align child div?   Suppose you have 2 Div tag. And want the make inner tag center <div id="d1">      ...

Ads Inside Post

Powered by Blogger.

Arsip

Blog Archive