2014-04-12 65 views
0

我想使用php導入excel數據到mysql。我的程序工作沒有任何錯誤,如果我已經寫了excel文件的路徑爲硬編碼。但是,如果我當時正在嘗試使用上傳功能,則會遇到一個錯誤。我的錯誤如下。 :我想使用php導入excel數據到mysql使用php

文件名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文件。所以請幫助我解決這個問題。

回答

0

您正試圖打開之後不存在的臨時文件move_uploaded_file被調用。

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

您需要打開「upload /」。 $ _FILES [「file」] [「name」]。 (爲什麼不使用$文件名您創建了嗎?)

此外,你傳遞了一個不存在於$ DATA-相同的文件名>閱讀(),爲什麼要創建處理程序呢? $ data-> read()應該使用$ handle$ filename?檢查一下。


我想補充另一種選擇,MySQL有一個整潔的Excel工具/插件,表格直接連接到名爲的MySQL爲Excel文件。

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

+0

非常感謝。我的問題解決了。 –

0

$ allowedExts數組中是否存在「.tmp」?請包括腳本的其餘部分,並確定哪些行輸出「文件名C:\ wamp \ tmp \ php1FC.tmp不可讀」。

+0

編號在$ allowedExts中只有兩個值在那裏。 .xls和.xlsx –

+0

那麼,這是您的第一個潛在問題?上傳時文件擴展名是.xls還是.xlsx?該錯誤顯示一個.tmp擴展名文件。 – PressingOnAlways

+0

是的。上傳後我的文件擴展名是.xls。我只在.xls擴展名中導入excel文件。 –