2016-06-13 134 views
0

我有問題打開文件並讀取文件中的數據。 我設法選擇一個文件來讀取,但fopen()根本不縫以打開文件。 有一個按鈕來選擇/瀏覽文件(接縫工作),然後導入按鈕,應該打開文件並將數據導入到應用程序。將CSV文件導入到我的PHP

下面是一些代碼:

<?php 
require_once "inc/common.inc.php"; 
// and some other 
?> 

<!DOCTYPE html> 
<html lang="us"> 
<head> 
    <script> 
    function doImport() { 
    // Some checks that file is selected and fields to import are chosen. 
    selFileName = document.getElementById("ImportFile").value; 

    if (($handle = fopen(selFileName, "r")) !== FALSE) { 
     fgetcsv($handle); 
     while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
     $num = count($data); 
     for ($c=0; $c < $num; $c++) { 
      $col[$c] = $data[$c]; 
     } 
     } 
    } else alert("Error in file opening!"); 
    } 
    </script> 
</head> 

<body> 
<p> 
    <input type="file" name="ImportFile" id="ImportFile" title="Chose a CSV file" /> 
    <input type="button" value="Import" onclick="doImport()" title="Import data from selected CSV-file." /> 
</p> 
</body> 
</html> 

我從fopen()函數的部分什麼都沒有,沒有eaven錯誤消息。

+0

您在這裏混合使用PHP和Javascript。國際海事組織你應該研究事情的工作原理,閱讀一些教程,並再次嘗試。 –

+0

這可能會有所幫助http://stackoverflow.com/a/19309237/4178487 –

+0

可能重複的[在Severial網站上發現的Javascript示例關注fopen不適用於我](http://stackoverflow.com/questions/19309187/javascript -examples發現的-ON-severial站點-regardinf-的fopen - 是 - 不工作,爲-M) –

回答

0

您試圖使用JavaScript調用PHP函數。 JavaScript運行在客戶端,PHP運行在服務器端,如果沒有像Ajax這樣的東西,兩者不能混合。

嘗試通過_POST_GET研究Ajaxpass your data through a form