2011-08-31 64 views
1

我有這個PHP:爲什麼這個PHP fgetcsv()僅顯示csv的某些部分?

$handle = fopen($_FILES["csvfile"]["tmp_name"], "r"); 

while (($data = fgetcsv($handle, 5000, ",", '"')) !== FALSE) { 
    echo "<pre>"; 
    print_r($data); 
    echo "<pre>"; 
} 

<form method="post"> 
<input type="file" name="csvfile"> 
<input type="submit> 

的CSV是由HTML <input type="file">提供。現在它正在打印只有第一個值和逗號或每行的數組,而不是打印出每行的十一個數組的數組。我知道它做正確的事情,但不知何故搞亂了我的代碼,無法弄清楚我做了什麼。

這裏是CSV的例子:

"lorem","ipsum","dolor","sit","amet","consectetuer","adipiscing","elit","sed ","diam","nonummy", 
+0

-1修復您的標題。 –

+1

老兄,在哪裏輸入? – ajreal

+0

你確定你指定了分隔符嗎?發佈輸入數據的樣本也 – Quamis

回答

1

在你貼我得到一個不錯的格式化的結果相同的代碼

file_put_contents('tmp.tmp', 
'"lorem","ipsum","dolor","sit","amet","consectetuer","adipiscing","elit","sed ","diam","nonummy", 
"1lorem","1ipsum","1dolor","1sit","amet","consectetuer","adipiscing","elit","sed ","diam","nonummy", 
'); 

$handle = fopen("tmp.tmp", "r"); 

while (($data = fgetcsv($handle, 5000, ",", '"')) !== FALSE) { 
    echo "<pre>"; 
    print_r($data); 
    echo "<pre>"; 
} 

我得到:

陣列 ( [ 0] => lorem [1] => ipsum [2] => dolor [3] =>坐下 [4] =>阿梅特 [5] => consectetuer [6] => adipiscing [7] => ELIT [8] => sed的 [9] =>直徑 [10] => nonummy [11] => )

你很可能在別的地方做錯了什麼,而不是注意到它。

+0

啊......現在我明白我在做什麼 - 顯然我使用的文件的一個版本沒有這樣格式化。奇怪的。謝謝。 – chromedude