how to upload excel file in php?

Today, We want to share with you how to upload excel file in php.In this post we will show you how to import xlsx file into mysql database using php?, hear for how to display excel file in php we will give you demo and example for implement.In this post, we will learn about Import Excel File Data In Database Using PHP with an example.

How to upload excel file to php server from <input type=“file”>?

Example 1: index.php

<?php
if(isset($_POST["submit"]))
{

                $url='127.0.0.1:8080';
                $dbUser='root';
                $dbPasswrod='';
                $link_new=mysqli_connect($url,$dbUser,$dbPasswrod,"location");
          if(!$link_new){
          die('Could not Connect My Sql:' .mysqli_error());
		  }
          $file = $_FILES['file']['tmp_name'];
          $handle = fopen($file, "r");
          $c = 0;
          while(($allfiles = fgetcsv($handle, 1000, ",")) !== false)
                    {
          $fname = $allfiles[0];
          $lname = $allfiles[1];
          $query_s = "insert into excel(fname,lname) values ('$fname','$lname')";
          $stmt = mysqli_prepare($link_new,$query_s);
          mysqli_stmt_execute($stmt);

         $c = $c + 1;
           }

            if($query_s){
               echo "Good Luck sucess";
             } 
		 else
		 {
            echo "Sorry! Unable to impo.";
          }

}
?>
<!DOCTYPE html>
<html>
<body>
<form enctype="multipart/form-data" method="post" role="form">
    <div class="dsp 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>
    </div>
    <button type="submit" class="btn btn-default" name="submit" value="submit">Upload</button>
</form>
</body>
</html>

PHP – import excel file into mysql database tutorial

db_config.php

<?php
	$dbHost = "127.0.0.1:8080";
	$dbDatabase = "h_php";
	$dbPasswrod = "root";
	$dbUser = "root";
	$mysqli = new mysqli($dbHost, $dbUser, $dbPasswrod, $dbDatabase);
?>

Create index.php file

<!DOCTYPE html>
<html>
<head>
	<title>Excel Uploading PHP - www.pakainfo.com</title>
	<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>


<div class="container">
	<h2>Excel Upload</h2>


	<form method="POST" action="excelUpload.php" enctype="multipart/form-data">
		<div class="dsp form-group">
			<label>Upload Excel File</label>
			<input type="file" name="file" class="form-control">
		</div>
		<div class="dsp form-group">
			<button type="submit" name="Submit" class="btn btn-success">Upload</button>
		</div>
		<p>Download Demo File from here : <a href="demo.ods"><strong>Demo.ods</strong></a></p>
	</form>
</div>


</body>
</html>

Create excelUpload.php file

<?php


require('library/php-excel-reader/excel_reader2.php');
require('library/SpreadsheetReader.php');
require('db_config.php');


if(isset($_POST['Submit'])){


  $mimes = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.oasis.opendocument.spreadsheet'];
  if(in_array($_FILES["file"]["type"],$mimes)){


    $fullpathofpicstore = 'uploads/'.basename($_FILES['file']['name']);
    move_uploaded_file($_FILES['file']['tmp_name'], $fullpathofpicstore);


    $Reader = new SpreadsheetReader($fullpathofpicstore);


    $totalSheet = count($Reader->sheets());


    echo "You have total ".$totalSheet." sheets".


    $html="<table border='1'>";
    $html.="<tr><th>Title</th><th>Description</th></tr>";

    for($i=0;$i<$totalSheet;$i++){


      $Reader->ChangeSheet($i);


      foreach ($Reader as $product)
      {
        $html.="<tr>";
        $title = isset($product[0]) ? $product[0] : '';
        $description = isset($product[1]) ? $product[1] : '';
        $html.="<td>".$title."</td>";
        $html.="<td>".$description."</td>";
        $html.="</tr>";


        $query = "insert into products(title,description) values('".$title."','".$description."')";


        $mysqli->query($query);
       }


    }


    $html.="</table>";
    echo $html;
    echo "<br />Data Inserted in dababase";


  }else { 
    die("<br/>Sorry, File type is not allowed. Only Excel file."); 
  }


}


?>

I hope you get an idea about How to Import Excel File into Mysql Database Table in PHP?.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.