Step-1 : Initially create a destination folder
uploads
to save all the uploaded files.
Step-2 : Create the controller
uploadfile.php
inside the folder
application\controllers in CodeIgniter and enter the below code.
[php]
<?php
class uploadfile extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
//index function
function index()
{
//load file upload form
$this->load->view('upload_file_view');
}
//file upload function
function upload()
{
//set preferences
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'txt|pdf';
$config['max_size'] = '100';
//load upload class library
$this->load->library('upload', $config);
if (!$this->upload->do_upload('filename'))
{
// case - failure
$upload_error = array('error' => $this->upload->display_errors());
$this->load->view('upload_file_view', $upload_error);
}
else
{
// case - success
$upload_data = $this->upload->data();
$data['success_msg'] = '<div class="alert alert-success text-center">Your file <strong>' . $upload_data['file_name'] . '</strong> was successfully uploaded!</div>';
$this->load->view('upload_file_view', $data);
}
}
}
?>
[/php]
In the above controller there is a method
upload() where the actual code for the file uploading takes place. The library file
upload is loaded initially using
$this->load->library('upload', $config);
statement in which array
$config possess the preferences(like file size, path,extensions) that controls the uploading process.
When the file is submitted,
upload() method of controller invokes and validation of file takes place.If any error occurs during validation, file uploading stops and if no error, file will be uploaded successfully.
Below are few methods of 'upload' class library in the controller:
$this->upload->do_upload()
: This method uploads the selected file to the destination folder.Providing parameters to this method is optional and if not declared, then it expects the file from the input field with name userfile. If other name is given to 'userfile', then pass the field name as parameter to the do_upload() function.
$this->upload->display_errors()
: Error message is returned by this method if the do_upload() method returns false.
$this->upload->data()
: An array of data is returned related to file uploaded like the file name, path, size etc.
Step-3 : Create the view file
upload_file_view.php
inside
application\views folder. This view file contains a simple upload form with file input and a submit button.
[html]
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CodeIgniter File Upload Form</title>
<!-- load bootstrap css file -->
<link href="<?php echo base_url("assets/bootstrap/css/bootstrap.css"); ?>" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 well">
<legend>CodeIgniter File Upload Demo</legend>
<?php echo form_open_multipart('uploadfile/upload');?>
<fieldset>
<div class="form-group">
<div class="row">
<div class="col-md-12">
<label for="filename" class="control-label">Select File to Upload</label>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-12">
<input type="file" name="filename" size="20" />
<span class="text-danger"><?php if (isset($error)) { echo $error; } ?></span>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-12">
<input type="submit" value="Upload File" class="btn btn-primary"/>
</div>
</div>
</div>
</fieldset>
<?php echo form_close(); ?>
<?php if (isset($success_msg)) { echo $success_msg; } ?>
</div>
</div>
</div>
</body>
</html>
[/html]
Output :