2016-04-05 17:40
浏览 92


OK, here's what I've got so far:

<table class="tftable" border="1">
    <tr><th>Header 1</th><th>Header 2</th><th>Header 3</th><th>Header 4</th><th>Header 5</th><th>Header 6</th><th>Header 7</th></tr>
        $csv_file = CSV_PATH . "test.csv";
        if(($handle=fopen($csv_file,"r"))!== FALSE){
                $num = count($data);

                echo "<tr><th>".$col1."</th><th>".$col2."</th><th>".$col3."</th><th>".$col4."</th><th>".$col5."</th><th>".$col6."</th><th>".$col7."</th></tr>";

So I run this and ($data=fgetcsv($handle,1000,",")) is coming out FALSE I think, but not sure why, and so only the headers get printed to the page.

Got rid of fgetcsv($handle);, but only prints one line, and I checked the file contents of my .csv file and this is what I have:


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsjpqpdm620596
    dsjpqpdm620596 2016-04-05 18:17

    Get rid of this line:


    That is:

    if(($handle=fopen($csv_file,"r"))!== FALSE){
         while ( ( $data = fgetcsv($handle, 1000, ",") ) !== FALSE ) {

    If this only prints one line, check the line endings on the csv. Make sure they are /n or /r/n

    Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.

    From which I'd assume the csv may have been generated in a mac. I am not positive, but I believe what was happening was that your first fgetcsv read the first (and only) line of your csv, so the second one, in the while loop, correctly noted there was no further line to read, so returned FALSE, thus terminating the loop before you started.

    If the csv is automatically generated, you'd want to correct this issue on that end of things, if it's created on say, a Mac, ideally the tool that created it is corrected to use unix style line endings. If that can't be readily done, just open the csv in a code editor, configured to save/convert line ending to unix style, and save it prior to loading.

    LF: Multics, Unix and Unix-like systems (Linux, OS X, FreeBSD, AIX, Xenix, etc.), BeOS, Amiga, RISC OS, and others[1]

    CR: Commodore 8-bit machines, Acorn BBC, ZX Spectrum, TRS-80, Apple II family, Mac OS up to version 9, and OS-9

    I was curious, it looks like Macs up to OS 9 used , but OS X uses as you'd expect. Note that windows uses which works fine since is ignored I believe and is present.

    点赞 评论