In this tutorial we will learn how to upload file with restriction on the type of file with the help of Codeigniter's upload class in the Codeigniter's library. With the help of this library we can set our preferences as per the uploading file type, size, dimension (in case of images) is concerned.
As Codeigniter follows the MVC architecture. Here we will create a “Controller” and a “View”. We are not working on model over here as we just want to get the request from the View regarding the file that we want to upload and process the requested data in the Controller and then we will render the information of the upload file which we can also save in the database. We go through the following steps to upload a file :-
Step 1: Lets create a View with name say "upload_view.php" under the directory “exampleupload/application/views/". This page will display form that will have an option to upload a file.
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?> <!-- Here the error message will be displayed -->
<?php echo form_open_multipart('upload/do_upload');?>
<?php echo "<input type='file' name='userfile' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>"?>
</body>
</html>
Step 2: Create a Controller named “upload”.Now we will write the functions that we will be using to render the “Upload From” page (i.e. upload_view.php) and the action “upload_file” that will handle the further processing.
public function __construct() {
parent::__construct();
//Below we are using the form helper and url helper
$this->load->helper(array('form', 'url'));
}
public function index(){
$this->load->view('upload/upload_view.php',array('error'=>''));
}
public function upload_file(){
// Below we will set file preferences that we want to upload.
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "pdf|jpg|png|gif|jpeg",
'overwrite' => TRUE,
'max_size' => "3048000",
'max_height' => "786",
'max_width' => "1024"
);
// Below we are loading the upload library to initialize an Upload class:
$this->load->library('upload',$config);
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload/upload_success',$data);
}else{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload/upload_view', $error);
}
}
Step 3: On successful upload of the file we will display a message with the data related to the upload file.
<html>
<head>
<title>Upload a file</title>
</head>
<body>
<h3>Your file is uploaded successfully!</h3>
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload/index', 'Click to upload another file..!'); ?></p>
</body>
</html>
Step 4: Finally we will create a folder in the root directory of our project. Let say my project folder name is exampleupload. Hence we will create a folder such as “exampleupload/upload”
Now on our browser address bar we will write the following path
1- localhost/exampleupload/upload/index
2- Upload the file
We will get the following output:
Output:
Your file is uploaded successfully!
file_name: bird.jpeg
file_type: image/jpeg
file_path: /var/www/html/exampleupload/uploads/
full_path: /var/www/html/exampleupload/uploads/bird.jpeg
raw_name: bird
orig_name: bird.jpeg
client_name: bird.jpeg
file_ext: .jpeg
file_size: 9.66
is_image: 1
image_width: 280
image_height: 160
image_type: jpeg
image_size_str: width="280" height="160"
Click to upload another file..!
Also check the folder in "exampleupload/uploads" and find the upload file with the name bird.jpeg
Note: To learn how to upload Multiple Files in CodeIgniter 2.0 Using Customized Library Class click the link below:
Upload Multiple Files in CodeIgniter 2.0 Using Customized Library Class
0 Comment(s)