WPITECH
Image default
PHP

How to Upload Image and File in CodeIgniter

File uploading is an important procedure for many apps. Image and file upload in CodeIgniter powered app is a simple component that handles all the upload process.

In this tutorial, we are going to discuss the process of creating a CodeIgniter based file upload component that could be used to easily upload images and other files. You can restrict any file size and type throughout the upload process.

1. Create a Folder

The very first and the initial step is to create two folders that are the base of the upload process. The first folder is the destination folder that receives the uploaded files and the next folder will go to the root of the CI installation that creates a folder term as “uploads”.

You can now go to the view folder that is located at the root of the CI installation and create two “view” files. Name these files file_view.php that displays the form containing the upload fields and upload_success.php that displays the successful message when upload completed.

2. Create a File in the Controller Folder

Now the other step is to create a file in the controller folder and name it as upload_controller.php. Next, I will load a library in this file for initializing the upload class using the following code.

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

3. Set File Upload Preferences

You have to set the preferences for the file upload process using the controller function do_upload(). This function consists of the following code.

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';

Using this function you can easily restrict the upload path, file types, size and dimensions of an image.

This function will perform when you call the file_view.php file. If you are using a staging domain then use the

URL: your-domain/ci_demo/index.php/upload_controller/file_view

OR

If you are using localhost then use this

loclhost: localhost/ci_demo/index.php/upload_controller/file_view

4. Form Helper

The file upload needs a multipart form. That’s why I have included a form helper in the controller using the following code.

$this->load->helper(array('form', 'url'));

5. Structural View

Here I have created a file in the view folder term as custom_view.php for the structure of the image upload. Add the following code into the file.

<html>
<head>
    <title>File Upload In Codeigniter</title>
</head>
<body>
<?php echo $error;?> 
<?php echo form_open_multipart('upload_controller/do_upload');?>
<?php echo "<input type='file' name='userfile' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>"?>
</body>
</html>

Next, create a new file upload_controller.php in the controller folder and add the following code:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function custom_view(){
$this->load->view('custom_view', array('error' => ' ' ));
}
public function do_upload(){
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success',$data);
}
else
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('custom_view', $error);
}
}
}
?>

Note that the array $config of the do_upload() consist element named allowed_types. You can easily change the allowed file types using the parameters of this element. Just take an example, If you want to allow a range of file you can use the following structure:

'allowed_types' => "gif|jpg|jpeg|png|iso|dmg|zip|rar|doc|docx|xls|xlsx|ppt|pptx|csv|ods|odt|odp|pdf|rtf|sxc|sxi|txt|exe|avi|mpeg|mp3|mp4|3gp",

6. Success Message

Now the next and the last step is to display the upload successful message. Add the following code in upload_success.php file that is located in the view folder.

<html>
<head>
    <title>Success Message</title>
</head>
<body>
<h3>Congragulation You Have Successfuly Uploaded</h3>
<!-- Uploaded file specification will show up here -->
<ul>
    <?php foreach ($upload_data as $item => $value):?>
    <li><?php echo $item;?>: <?php echo $value;?></li>
    <?php endforeach; ?>
</ul>
<p><?php echo anchor('upload_controller/file_view', 'Upload Another File!'); ?></p>
</body>
</html>

 

Related posts

What’s new in PHP 7.3?

WPITECH

How to Pass Data From Controller to View in CodeIgniter

WPITECH