简体   繁体   中英

I want to import excel data into mysql using php

I want to import excel data into mysql using php. My program works without any error if I have written path of excel file as hardcoded. But if I am trying to use it using upload function at that time I am facing one error. My error is as follws. :

filename C:\\wamp\\tmp\\php1FC.tmp is not readable

I am also providing my code for reference :

include 'config.php'; 
require_once 'Excel/reader.php';
$allowedExts = array("xls","xlsx");
$temp = explode(".", $_FILES["file"]["name"]);

     if (in_array($extension, $allowedExts))
          {
          if ($_FILES["file"]["error"] > 0)
            {
            echo "Error: " . $_FILES["file"]["error"] . "<br>";
            }
          else
            {
            $filename=$_FILES["file"]["name"] ;
            $filetype=$_FILES["file"]["type"] ;
            $filesize=$_FILES["file"]["size"] ;
            $filetemp=$_FILES["file"]["tmp_name"];


              if (file_exists("upload/" . $_FILES["file"]["name"]))
              {
                    echo $_FILES["file"]["name"] . " already exists. ";
              }
              else
              {
                    move_uploaded_file($_FILES["file"]["tmp_name"],
                    "upload/" . $_FILES["file"]["name"]);
                    echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
              }

                $handle = fopen($filetemp, "r");

                $data = new Spreadsheet_Excel_Reader();
                $data->read($filetemp);
            $numr=$data->sheets[0]['numRows'];  
                for ($i = 2; $i <= $numr ; $i++) 
                {
                $gender=$data->sheets[0]['cells'][$i][1];
                        $txtname=$data->sheets[0]['cells'][$i][2];
                $txtusername = $data->sheets[0]['cells'][$i][3];
                        $txtphone=$data->sheets[0]['cells'][$i][4];
                        $txtlandno=$data->sheets[0]['cells'][$i][5];               
                        $txtwing=$data->sheets[0]['cells'][$i][6];
                        $txtemail=$data->sheets[0]['cells'][$i][7];               
                        $txtflat=$data->sheets[0]['cells'][$i][8];
                        $intercom=$data->sheets[0]['cells'][$i][9];
                        $userstatus=$data->sheets[0]['cells'][$i][10];
                        $livestatus=$data->sheets[0]['cells'][$i][11];                
                        $flattype=$data->sheets[0]['cells'][$i][12];
                        $area1=$data->sheets[0]['cells'][$i][13];
                        $txtbuilding=$data->sheets[0]['cells'][$i][14];
                        $housingloan=$data->sheets[0]['cells'][$i][15];
                        $txtparking=$data->sheets[0]['cells'][$i][16];
                        $principalopBal=$data->sheets[0]['cells'][$i][17];
                        $interestBal=$data->sheets[0]['cells'][$i][18];
                        $servicetax=$data->sheets[0]['cells'][$i][19];

                        $txtgym=$data->sheets[0]['cells'][$i][20];
                        $txtcable=$data->sheets[0]['cells'][$i][21];
                        $txtswim=$data->sheets[0]['cells'][$i][22];
                        $txtclub=$data->sheets[0]['cells'][$i][23];
                        $unittype=$data->sheets[0]['cells'][$i][24];

I dont understand whats the problem. I want to perform this importing excel data functionality. And I want user can upload its own excel file for this. So please help me in this problem.

Is ".tmp" in the $allowedExts array? Please include the rest of the script and identify what line is outputting "filename C:\\wamp\\tmp\\php1FC.tmp is not readable".

You're trying to open the temporary file that doesn't exist anymore after move_uploaded_file is called.

move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);

You need to open "upload/" . $_FILES["file"]["name"] . (Why not use $filename that you created it ?)

In addition, you're passing the same filename that doesn't exist to $data->read() , why create the handler then ? $data->read() should use $handle or the $filename ? Check that too.


Just to add another option, MySQL has a neat Excel tool/plugin that connects the tables directly to the file called MySQL for Excel .

More information: http://dev.mysql.com/doc/refman/5.7/en/mysql-for-excel.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM