2016-09-27 58 views
0

我想在一個PHP頁面運行時上傳CSV內容。我不想要任何瀏覽按鈕來上傳CSV。每當頁面運行時,頁面都應該找到PHP頁面中已經定義了路徑的CSV,並且應該將內容插入到表格中。現在我收到與fopen有關的錯誤。CSV上傳無瀏覽按鈕

這裏是我的代碼

<?php 

//database connection details 
$connect = mysql_connect('localhost', 'root', ''); 

if (!$connect) { 
    die('Could not connect to MySQL: ' . mysql_error()); 
} 

//your database name 
$cid = mysql_select_db('test', $connect); 

// path where your CSV file is located 


define('CSV_PATH', 'D:/xamp/htdocs/test/'); 

// Name of your CSV file 
$csv_file = CSV_PATH . "test.csv"; 

echo $csv_file; 
if (($handle = fopen($csv_file, "r")) !== FALSE) { 
    fgetcsv($handle); 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
     $num = count($data); 
     for ($c = 0; $c < $num; $c++) { 
      $col[$c] = $data[$c]; 
     } 

     $col1 = $col[0]; 
     $col2 = $col[1]; 
     $col3 = $col[2]; 
     $col4 = $col[3]; 
     $col5 = $col[4]; 
     $col6 = $col[5]; 


     // SQL Query to insert data into DataBase 
     $query = "INSERT INTO testcsv(Line,Part No,Make,Model,Year,Part Type) VALUES('" . $col1 . "','" . $col2 . "','" . $col3 . "','" . $col4 . "','" . $col5 . "','" . $col6 . "')"; 
     $s = mysql_query($query, $connect); 
    } 
    fclose($handle); 
} 

echo "File data successfully imported to database!!"; 
mysql_close($connect); 
?> 

我收到此錯誤

警告:的fopen(d:/xamp/htdocs/test/test.csv):未能打開流:沒有第22行D:\ xamp \ htdocs \ test \ test.php中的文件或目錄 成功導入數據庫的文件數據!

任何人都可以幫助我嗎?

+0

什麼是不清楚「沒有這樣的文件或目錄」? – Quentin

+0

術語「上傳」意味着將數據從客戶端發送到服務器。您是否意識到服務器上的文件路徑與客戶端上的文件路徑不是(通常)相同的東西?他們通常是兩個不同的電腦。您的測試環境是否只使用一臺計算機?生產系統是否也只使用一臺計算機? – Quentin

+0

[PHP - 未能打開流:沒有這樣的文件或目錄]可能的重複(http://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or- directory ) –

回答

0

我不知道爲什麼你會得到那個特定的錯誤 - 有人可能會認爲該文件不存在,或者該目錄不可讀,但你正在使用現在已棄用的mysql_函數,並直接在sql中嵌入變量 - 從而使其容易受到sql注入的影響。但是,這看起來只是一個可能不是問題的測試。

對於這種類型的事情的首選方法是結合使用兩種mysqliPDOprepared statements - 下面是一個如何實現這樣的例子 - 我用不同的數據和數據庫的細節測試,這和它似乎工作精細。

define('CSV_PATH','D:/xamp/htdocs/test/'); 
$filepath = CSV_PATH . "test.csv"; 

/* database connection details */ 
$host = 'localhost'; 
$uname = 'xxx'; 
$pwd = 'xxx'; 
$db  = 'xxx'; 

/* create db connection */ 
$con = new mysqli($host, $uname, $pwd, $db); 

/* construct required sql statement */ 
$sql='insert into `testcsv` (`Line`,`Part No`,`Make`,`Model`,`Year`,`Part Type`) values (?,?,?,?,?,?)'; 

/* create prepared statement */ 
$stmt=$con->prepare($sql); 


if(!$stmt){ 

    echo 'error preparing sql statement!'; 
    $con->close(); 

} else { 

    /* bind the columns to variables which will be populated later */ 
    /* use "i" for integer and "s" for string values */ 
    $stmt->bind_param('ssssss', $line,$part,$make,$model,$year,$type); 

    /* access csv file */ 
    $file=new SplFileObject($filepath); 

    /* Process each row of the csv file */ 
    while(!$file->eof()) { 

     /* read the line into a variable */ 
     $data=$file->fgetcsv(); 

     if(!empty($data)){ 
      /* assign a variable to each field value for this row */ 
      list($line,$part,$make,$model,$year,$type)=$data; 

      /* execute statement with the now defined variables */ 
      $stmt->execute(); 
     } 
    } 

    /* tidy up */ 
    $stmt->close(); 
    $con->close(); 

    echo 'database updated with new records from csv'; 
} 
+0

答案應該回答這個問題。你花了一半的時間說你不知道答案,然後在應該發表評論的地方留下了很多空間。 – Quentin