PHP MVC CodeIgniter Laravel Core PHP
Need a Website Or Web Application.Contact : +91 9437911966 (Whatsapp) Note: Paid Service

How to import excel and CSV file using PHP MVC, MySQL

In this example we using Models, Views, Controller Structure for import excel and CSV file in PHP MVC.

To import excel and CSV file first we have to create a Controller file.


class Hello extends Controller {
	function __construct() {
		/* Session::init(); */
	function import(){
					$file = $_FILES['file']['tmp_name'];
					$handle = fopen($file, "r");
					$c = 0;/*
					while(($filesop = fgetcsv($handle, 1000, ",")) !== false)*/
						$name = $filesop[0];
						$email = $filesop[1];
						$contact = $filesop[2];
						$data = array(
							'id'=> null,
							'name' =>$name,
							'email' =>$email,
							'contact' => $contact
						if($c<>0){					/*SKIP THE FIRST ROW*/
						$c = $c + 1;
					echo "Data imported successfully !"	;

Here is the model file which we are using for inserting data to database.

The file index_model.php is created under Models folder



class Hello_Model extends Model
	public function __construct()
	public function submit_index($data)
		$this->db->insert('user_data', $data);

Here is the view file index.php which inside views folder contains the form.


<!DOCTYPE html>
<form enctype="multipart/form-data" method="post" role="form">
	<div class="form-group">
	<label for="exampleInputFile">File Upload</label>
	<input type="file" name="file" id="file" size="150">
	<p class="help-block">Only Excel/CSV File Import.</p>
	<button type="submit" class="btn btn-default" name="save" value="submit">Upload</button>


<form action="<?php echo URL; ?>hello/verification" method="post" onsubmit="return confirm('Do you really want to submit the form?');">
	<center><input class="" name="otp" id="name" placeholder="Enter OTP"><br><br>
	<button type="submit" class="btn-info" value="submit" name="save">Submit</button>
	<button type="submit" class="btn-info" value="submit" name="resend">Resend</button></center>
Path: localhost/project_folder_name/view_folder_name/view_filename
Example: localhost/mvc/hello/import