简体   繁体   English

我想使用php将Excel数据导入mysql

[英]I want to import excel data into mysql using php

I want to import excel data into mysql using php. 我想使用php将Excel数据导入mysql。 My program works without any error if I have written path of excel file as hardcoded. 如果我已将excel文件的路径写为硬编码,则我的程序可以正常工作。 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 文件名C:\\ wamp \\ tmp \\ php1FC.tmp不可读

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. 我要执行此导入excel数据功能。 And I want user can upload its own excel file for this. 我希望用户可以为此上传自己的Excel文件。 So please help me in this problem. 所以请帮我解决这个问题。

Is ".tmp" in the $allowedExts array? 是$ allowedExts数组中的“ .tmp”吗? Please include the rest of the script and identify what line is outputting "filename C:\\wamp\\tmp\\php1FC.tmp is not readable". 请包括脚本的其余部分,并确定正在输出的行“文件名C:\\ wamp \\ tmp \\ php1FC.tmp不可读”。

You're trying to open the temporary file that doesn't exist anymore after move_uploaded_file is called. 您正在尝试打开调用move_uploaded_file之后不再存在的临时文件。

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

You need to open "upload/" . 您需要打开“ upload /”。 $_FILES["file"]["name"] . $ _FILES [“ file”] [“ name”] (Why not use $filename that you created it ?) (为什么不使用您创建的$ filename ?)

In addition, you're passing the same filename that doesn't exist to $data->read() , why create the handler then ? 另外,您要将不存在的文件名传递给$ data-> read() ,那么为什么要创建处理程序呢? $data->read() should use $handle or the $filename ? $ data-> read()应该使用$ handle$ 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 . 为了增加另一个选择,MySQL提供了一个简洁的Excel工具/插件,可以将表直接连接到名为MySQL for Excel的文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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