0

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?

0

1 Answer 1

2

When a file is uploaded in PHP, it is put into temp folders.

In your code, there is no call for moving the uploaded file from temporary location into the desired one. Instead you are only checking $_FILES["excelFile"]["tmp_name"] and then directly using $_FILES["excelFile"]["name"] which is obviously incorrect.

Use move_uploaded_file() function to move the file, see the docs: http://php.net/move_uploaded_file

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.