开发者

Why does this PHP fgetcsv() display only certain parts of the csv?

I have this PHP:

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

The CSV is provided by an html <input type="file">. Right now it is printing out arrays that are only the first value and a comma or each row instead of printing out an array of eleven values of each row. I got 开发者_开发问答it to do the right thing, but somehow messed up my code and can't figure out what I did.

Here is an example of the CSV:

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


On the same code you posted i get a nice formatted result

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>";
}

I get:

Array ( [0] => lorem [1] => ipsum [2] => dolor [3] => sit [4] => amet [5] => consectetuer [6] => adipiscing [7] => elit [8] => sed [9] => diam [10] => nonummy [11] => )

You're probaly doing something wrong somewhere else, and not noticing it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜