Home:ALL Converter>Excel file upload in PHP

Excel file upload in PHP

Ask Time:2015-12-13T19:59:23         Author:Vishal Varshney

Json Formatter

I am trying to upload Excel file using PHP. I am using WAMP server and written PHP code to upload Excel file in the same folder.

I have saved an Excel file so if I upload the saved file then it works fine but if I try to upload another Excel file from different location i.e. from desktop it is showing that abc.xls is not readable.

My code is as follows:

<?php
ini_set("display_errors",1);
require_once 'excel_reader2.php';
require_once 'db.php';

if(isset($_POST["btnImport"]))
                 {
                    if(!empty($_FILES["excelFile"]["tmp_name"]))
                     {
                        $fileupload = $_FILES["excelFile"]["name"];                          
                        $fileName = explode(".",$_FILES["excelFile"]["name"]);
                        if($fileName[1]=="xls"||$fileName[1]=="xlsx")
                        {
                        $data = new Spreadsheet_Excel_Reader($fileupload);
                        //echo "Total Sheets in this xls file: ".count($data->sheets)."<br /><br />";
                        $html="<table border='1'>";
                        for($i=0;$i<count($data->sheets);$i++) // Loop to get all sheets in a file.
                            {   
                                if(count(@$data->sheets[$i]['cells'])>0) // checking sheet not empty
                                {
                            //  echo "Sheet $i:<br /><br />Total rows in sheet $i  ".count($data->sheets[$i]['cells'])."<br />";
                                for($j=1;$j<=count($data->sheets[$i]['cells']);$j++) // loop used to get each row of the sheet
                                { 
                                $html.="<tr>";
                                for($k=1;$k<=count($data->sheets[$i]['cells'][$j]);$k++) // This loop is created to get data in a table format.
                                {
                                    $html.="<td>";
                                    @$html.=$data->sheets[$i]['cells'][$j][$k];
                                $html.="</td>";
                                }
                                    @$data->sheets[$i]['cells'][$j][1];
                                    @$ques = mysql_real_escape_string($data->sheets[$i]['cells'][$j][1]);
                                    $opt1 = mysql_real_escape_string($data->sheets[$i]['cells'][$j][2]);
                                    $opt2 = mysql_real_escape_string($data->sheets[$i]['cells'][$j][3]);
                                    $opt3 = mysql_real_escape_string($data->sheets[$i]['cells'][$j][4]);
                                    @$opt4 = mysql_real_escape_string($data->sheets[$i]['cells'][$j][5]);
                                    @$correct = mysql_real_escape_string($data->sheets[$i]['cells'][$j][6]);
                                    @$Level = mysql_real_escape_string($data->sheets[$i]['cells'][$j][7]);
                                    @$Category = mysql_real_escape_string($data->sheets[$i]['cells'][$j][8]);
                                    @$explain = mysql_real_escape_string($data->sheets[$i]['cells'][$j][9]);

$nithi = "INSERT INTO `question` VALUES (NULL,'".$ques."','".$opt1."','".$opt2."','".$opt3."','".$opt4."','".$correct."','".$Level."','".$Category."','".$explain."')";

if (!mysql_query($nithi,$connection))
{
  die('Error: ' . mysql_error());
}

$result = mysql_query("SET NAMES utf8");//the main trick
$cmd = "select * from TAMIL";
$result = mysql_query($cmd);

    }
    }

                 }}}}
                 if(@$result){
    echo "Questions uploaded successfully";
                 }
                 ?>

How can I upload Excel file from different location?

Author:Vishal Varshney,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/34250832/excel-file-upload-in-php
yy