UPLOAD FILE Using CODEIGNITER FRAMEWORK

Date posted :03/04/2019

Most importantly, in this tutorial we are going to upload the file using PHP- codeigniter framework. It has upload library for upload the file.

File upload using codeigniter Framework

pre-requesties

1)However create a uploads directory at the project root to store files.
2)After that, we need to prepare a simple form. It allow us to select the file.
3)In addition to that generate the controller for include file details.
4)Finally, create the model for database.

Step by step procedure:

The following steps are used to upload the file.

step1:

Create a directory name as ‘uploads’ And it should have the file permission of 777. Because, this is the most important step to allow permission on upload the file.

step 2:

Using a text editor, create a form and name it as fileview.php(application/views/). This will be used for rendering the html to user for allowing file upload functionality.

For example,

<html>
   <head>
      <title>My Form</title>
   </head>
   <body>
      <!-- <form action = ""> -->
      <?php echo $error;?>
      <?php echo form_open_multipart('Form/do_upload');?>
      <h5>firstname</h5>
      <input type = "text" name = "firstname" value="">
      <h5>gender</h5>
      <input type = "text" name = "gender" value="">  
      <h5>hobbies</h5>
      <input type = "text" name = "hobbies" value="">
      <h5>district</h5>
      <input type = "text" name = "district" value="">
      <h5>discription</h5>
      <input type = "text" name = "discription" value="">
      <h5>userfile</h5>
      <input type = "file" name = "userfile"  value="">
      
</tr> <?php echo form_close(); ?> </body> </html>

step 3:

Instantly, create a controller file and also name it as Form.php
File name : (application/controllers/ Form.php).

public function upload_control() { 
          $this->load->library('form_validation');
         $this->load->view('fileview', array('error' => ' ' )); 
      } 

   public function do_upload() { 
      #Used to load the model
      $this->load->model('Insert_model');
       #Used to load the form validation library  
      $this->load->library('form_validation');
       #Used to load the upload library
       $this->load->library('upload');

          #used to get the file details
         $config['upload_path'] = './uploads/'; 
         $config['allowed_types'] = 'php|gif|jpg|png|txt|doc|docx'; 
         $config['max_size']      = '1000'; 
         $config['max_width']     = '1024'; 
         $config['max_height']    = '768';  
         
         $this->upload->initialize($config);

           #used to upload the file name in to the folder(uploads)

         if ( ! $this->upload->do_upload('userfile')) { 
              #$error used to display about error
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('fileview', $error); 

         }
         else
          {         

           $data = array('upload_data' => $this->upload->data());
            $uploadData = $this->upload->data();
            #used to get the file name
            $userfile1 = $uploadData['file_name']; 
              
             $postData = array(
                'firstname' => $this->input->post('firstname'),
                'gender' => $this->input->post('gender'),
                  'hobbies' => $this->input->post('hobbies'),
                    'district' => $this->input->post('district'),
                      'discription' => $this->input->post('discription'),
                      'userfile' => $userfile1,
                      'discription' => $this->input->post('discription'),
                 ); 
                 # used to insert the values in to database 
              $insert = $this->Insert_model->insert($postData); 
          echo "Data inserted successfully";
           
            
         } 
        
         }
  • $this->load->model(‘Insert_model’); = Used to load the form model(database).
  • $this->load->library(‘form_validation’); = Used to load the form validation library.
  • $this->load->library(‘upload’); = Used to load the upload library for file upload.
  • $this->upload->do_upload(‘userfile’) =Used to upload the file name into the folder.
  • $error = array(‘error’ => $this->upload->display_errors()); = Used to diplay the error.
  • $this->load->view(‘fileview’, $error); = This will load the form with the error.

step4:

As a result of create a model, name it as Insert_model.php(application/controllers/ Insert_model.php).

<?php
class Insert_model extends CI_Model 
{
	public function insert($data = array()) 
	{
        $insert = $this->db->insert('form', $data);
    }
}
?>

conclusion:

Finally, The selected file name will show in the database and the file is uploaded to the folder with file name(uploads).

Thanks for using pheonixsolutions.

You find this tutorial helpful? Share with your friends to keep it alive.

Leave a Reply