2016-06-13 9 views
0

파일을 열고 파일의 데이터를 읽는 데 문제가 있습니다. 읽을 파일을 선택할 수 있지만 fopen()은 파일을 여는 데 솔기가 없습니다. 하나의 버튼을 사용하여 이음새가있는 파일을 선택/찾아 본 다음 가져 오기 버튼을 클릭하여 파일을 열고 응용 프로그램에 데이터를 가져와야합니다.PHP로 CSV 파일 가져 오기

<?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

여기에 Javascript와 PHP가 혼합되어 있습니다. IMO 당신은 일들이 어떻게 작동하는지 연구하고, 튜토리얼을 읽고 또 한번 시도해야합니다. –

+0

도움이 될 수 있습니다. http://stackoverflow.com/a/19309237/4178487 –

+0

[자바 스크립트 예제는 fopen 관련 페이지에서 발견되었습니다.] (http://stackoverflow.com/questions/19309187/javascript) -examples-found-on-severial-sites-regardinf-fopen-not-working-for-m) –

답변

0

JavaScript로 PHP 함수를 호출하려고합니다. JavaScript는 클라이언트 측에서 실행되며 PHP는 서버 측에서 실행됩니다. 두 개는 Ajax과 같이 섞어서 사용할 수 없습니다.

Ajax 또는 pass your data through a form_POST 또는 _GET을 통해 조사해보십시오.