0

I have a bunch of files with data formated as follows: int|int|string|string

I have some functions to get all files in a directory, format their names and make a select drop-down menu from that.

From that, the user selects the file he wants to open.

The choosen file (its filename) gets send via POST to a second php file that opens in an iframe right below the drop-down form.

These are the relevant contents of said file:

<table>
    <thead>
        <tr>
            <th>A:</th>
            <th>B:</th>
            <th>C:</th>
        </tr>
    </thead>
    <tbody>
<?php
$fl = $_POST["file"];
$currentfile = fopen("./dir/$fl","r");
if ($currentfile) {
    while (($line = fgets($currentfile)) !== false) {
        $n = sscanf($line, "%d|%d|%[^|]|%[^\n]", $a,$b,$c,$d);
        print "<tr><td>$a</td><td>$b</td><td>$d</td></tr>";
    }
    fclose($currentfile);
    } else {
        print "Error: Couldn't open file.<br>";
    }
?>
    </tbody>
</table>

Now somehow, the first line in each file isn't shown in the table generated by this, everything else is fine.

As an example, here's one file.

1|334|Item 1
2|837|Item 2
3|321|Item 3
4|124|Item 4
5|331|Item 5

etc...

And this is the output I get.

A:   B:   C:

2    837  Item 2
3    321  Item 3
4    124  Item 4
5    331  Item 5

etc...

Or in code:

<table>
    <thead>
        <tr>
            <th>A:</th>
            <th>B:</th>
            <th>C:</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td>2</td>
            <td>837</td>
            <td>Item 2</td>
        </tr>
        <tr>
            <td>3</td>
            <td>321</td>
            <td>Item 3</td>
        </tr>
        <tr>
            <td>4</td>
            <td>124</td>
            <td>Item 4</td>
        </tr>
        <tr>
            <td>5</td>
            <td>331</td>
            <td>Item 5</td>
        </tr>

        etc...

    </tbody>
</table>

So as you can see by above code, the td tags get printed, but there's no data in them, whereas the second set of td tags have the second line of data in them, as it should be, so why doesn't it read, scan and print the first line of the file?

J. H.
  • 79
  • 9

1 Answers1

1

I believe this happens because there is the "Byte order mark" in the beginning of your files. You need to check for it and replace it or remove it from the files.

Here is what worked for me:

while (($line = fgets($currentfile)) !== false) {
    $bom = pack('H*','EFBBBF');
    $line = preg_replace("/^$bom/", '', $line);

    $n = sscanf($line, "%d|%d|%[^|]|%[^\n]", $a, $b, $c, $d);
    print "<tr><td>$a</td><td>$b</td><td>$c</td></tr>";
}

Maybe this code does not work on your system and you need to try some other code to remove the BOM. Try also this:

$line = str_replace("\xEF\xBB\xBF",'',$line);

Hope it helps.

Reference

Jannes Botis
  • 10,614
  • 3
  • 18
  • 35
  • Yes, that was the cause of my problem. Thank you very much. Just converted the files from UTF8-BOM to UTF8. – J. H. Feb 01 '17 at 13:31