2016-06-13 161 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