This tutorial will show you how to integrate multiple PHP files with the CodeIgniter framework. By following these simple steps, you’ll be able to create complex web applications more easily and efficiently. Let’s get started.
Firstly, you have to set an environment to use CodeIgniter framework in PHP, follow the below steps to understand easily:
- Download and install PHP in your computer.
- Install Xampp in your computer and start Apache and MySql.
- Now create a local host on localhost/myPhp.
- Install sumlime Text editor to write Php code effectively.
Table of Contents
Step #1
<div class="container">
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<div class="card-mt-4">
<div class="card-header">
upload files
</div>
<div class="card-body">
<form action="<?=base_url('welcome/upload/')?>" enctype="multipart/form-data" method="post">
<div class="mb-3">
<label class="form-label"id="uploadFile">select files
</label>
<input type="file" name="uploadedfiles[]">
</div>
</div>
</div>
<div class="col-md-4"></div>
</div>
</div>
Output

Step#2
Set the file name, type tmp_name and errors.
<?php
defined('BASEPATH') OR exit ('no direct script access allowed');
class php1 extends CI_controller{
public function index()
{
$this->loads-.view('prog_file');
}
function upload()
{
$countFiles = count($_FILES['uploadedFiles']['name']);
$countUploadedFiles=0;
$countErrorUploadedFiles=0;
for($i=0;$i<$countFiles;$i++)
{
$_FILES[''uploadedFiles']['name']= $_FILES['uploadedFiles']['name'])['$i'];
$_FILES[''uploadedFiles']['type']= $_FILES['uploadedFiles']['type'])['$i'];
$_FILES[''uploadedFiles']['size']= $_FILES['uploadedFiles']['size'])['$i'];
$_FILES[''uploadedFiles']['tmp_name']= $_FILES['uploadedFiles']['tmp_name'])['$i'];
$_FILES[''uploadedFiles']['error']= $_FILES['uploadedFiles']['error'])['$i'];
}
}
}
Step#3
function upload()
{
$countFiles = count($_FILES['uploadedFiles']['name']);
$countUploadedFiles=0;
$countErrorUploadedFiles=0;
for($i=0;$i<$countFiles;$i++)
{
$_FILES[''uploadFile']['name']= $_FILES['uploadedFiles']['name'])['$i'];
$_FILES[''uploadedFiles']['type']= $_FILES['uploadedFiles']['type'])['$i'];
$_FILES[''uploadFiles']['size']= $_FILES['uploadedFiles']['size'])['$i'];
$_FILES[''uploadFiles']['tmp_name']= $_FILES['uploadedFiles']['tmp_name'])['$i'];
$_FILES[''upload Files']['error']= $_FILES['uploadedFiles']['error'])['$i'];
$uploadStatus = $this->uploadFile('uploadFile');
if($uploadStatus!=false)
{
$countErrorUploadedFiles++;
}
else
{
$countUploadedFiles++;
}
}
}
function uploadeFile($name)
{
$uploadedPath='uploads/images/';
if(!is_dir($uploadedPath))
{
mkdir ($uploadedPath,0777,TRUE);
}
$config['upload_path']=$uploadPath;
$config['allowed_types']='jpeg|JPEG|jpg|JPG|png|PNG';
$config['encrypt_name']=TRUE;
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload($name))
{
$filesData= $this->upload->data();
return $fileData['file_name'];
}
else
{
return false;
}
}
}
step#4
Connect your localhots myPhp database images table, where your images will be store.
- Create file as Upload_file.
<?php
class upload_file extends CI_Model{
function upload_data($data)
{
#this->db->insert('images',$data);
}
}
- Connect your file to your main file where you have code the main functionality of your form //upload files form.
if($uploadStatus!=false)
{
$countErrorUploadedFiles++;
$this->load->model('upload_file');
$data = array(
'img_path'=>$uploadStatus,
'upload_time=>date('Y-m-D H:i:s')),
};
$this->upload_file->upload_data($data);
else
{
$countUploadedFiles++;
}
}
$this->session->set_flashdata('message','Files uploaded. Successfull Files Uploaded:'.$countUploadedFiles. 'and Error uploading files:'.$countErrorUploadedFiles);
redirect(base_url('prog_file/index'));
}
- Now, set the functionality to upload multiple files and alert message when files are uploaded successfully.
function uploadeFile($name)
{
$uploadedPath='uploads/images/';
if(!is_dir($uploadedPath))
{
mkdir ($uploadedPath,0777,TRUE);
}
$config['upload_path']=$uploadPath;
$config['allowed_types']='jpeg|JPEG|jpg|JPG|png|PNG';
$config['encrypt_name']=TRUE;
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload($name))
{
$filesData= $this->upload->data();
return $fileData['file_name'];
}
else
{
return false;
}
}
}
Output

To know more frameworks in PHP visit the article, Top 10 PHP Framework in 2021.
FAQs
How many libraries are there in CodeIgniter?
The system folder contains all of CodeIgniter’s libraries. However, you may build any other library that you want to use in your program. It is not necessary to have a limit for libraries.
What is default controller in CodeIgniter?
When a URI is not supplied, CodeIgniter may be instructed to load a default controller, just like when your site root URL is requested. To specify a default controller, open your application/config/routes. php file and set this variable: $route[‘default_controller’] = ‘ Blog ‘;
Conclusion
Many web applications require complex functionality. For that, it is recommended to use a framework like CodeIgniter. With this framework, you can build and deploy your apps efficiently in no time. We have just covered how to integrate multiple PHP files with the CodeIgniter framework, so stay tuned for our next post where we will explain how to incorporate advanced coding techniques into CodeIgniter apps.