【问题标题】:I want to import excel data into mysql using php我想使用 php 将 excel 数据导入 mysql
【发布时间】:2014-04-12 06:13:54
【问题描述】:

我想使用 php 将 excel 数据导入 mysql。如果我将 excel 文件的路径写为硬编码,我的程序可以正常工作。但是,如果我当时尝试使用上传功能使用它,我将面临一个错误。我的错误如下。 :

文件名 C:\wamp\tmp\php1FC.tmp 不可读

我还提供我的代码供参考:

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];

我不明白是什么问题。我想执行这个导入 excel 数据的功能。我希望用户可以为此上传自己的 excel 文件。所以请帮我解决这个问题。

【问题讨论】:

    标签: php mysql phpexcel


    【解决方案1】:

    您正在尝试打开调用 move_uploaded_file 后不再存在的临时文件。

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

    您需要打开 "upload/" 。 $_FILES["文件"]["名称"]。 (为什么不使用您创建的 $filename?)

    此外,您将不存在的相同文件名传递给 $data->read(),为什么还要创建处理程序? $data->read() 应该使用 $handle 还是 $filename ?也检查一下。


    只是添加另一个选项,MySQL 有一个简洁的 Excel 工具/插件,可将表直接连接到名为 MySQL for Excel 的文件。

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

    【讨论】:

      【解决方案2】:

      “.tmp”在 $allowedExts 数组中吗?请包含脚本的其余部分并确定输出“文件名 C:\wamp\tmp\php1FC.tmp 不可读”的行。

      【讨论】:

      • 没有。在 $allowedExts 中只有两个值。 .xls 和 .xlsx
      • 嗯,这可能是您的第一个问题吗?上传时的文件扩展名是 .xls 还是 .xlsx?错误显示一个 .tmp 扩展文件。
      • 是的。上传后我的文件扩展名为 .xls。我只在 .xls 扩展名中导入 excel 文件。
      • 不明白是什么问题。
      • 导入一个 xls 文件并不意味着它仍然是一个 .xls 文件扩展名。 php 可能会将其作为 .tmp 文件上传,您可能需要负责将其重命名为正确的扩展名。您必须调试并找到引发错误的行。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      相关资源
      最近更新 更多