File Uploading
How to upload file on server

1- Create Folder suppose we create upload in root.
2- Create HTML Form that show file upload button, when submit form then show error or success.
3- Create Controller.

Create HTML Form save in view folder

<html>
<head>
<title>File Upload Form</title>
</head>
<body>

<?php
if(isset($error))
echo $error;
else{
echo "File Successfully Uploaded"; ?>
<ul>
<?php foreach ($upload_data as $item => $value){?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php
}
}
?>

</ul>
<form name="frm" action="<?php echo base_url(); ?>index.php/user/upload" enctype="multipart/form-data" method="post">
You can upload gif,jpg and png file
<input type="file" name="filename" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>

Create Controller and save in Controller folder

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller {

public function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
}

public function index()
{
$this->load->view('fileupload');
}

public function upload()
{
$file_config['upload_path'] = './upload/';
$file_config['allowed_types'] = 'gif|jpg|png';
$file_config['max_size'] = 100;
$file_config['max_width'] = 1500;
$file_config['max_height'] = 1200;

$this->load->library('upload', $file_config);

if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('fileupload', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('fileupload', $data);
}
}

}


Previous
Next