Skip to content

fgets() sometimes does not read a full line #10198

@sboden

Description

@sboden

Description

fgets() sometimes does not read a full line.

I'm reading old style fixed format ASCII files using PHP 8.0.26 (from the official docker image). The file only contains [a-zA-Z0-9], spaces and newlines. Every line in the file is exactly 559 characters long. The file gets downloaded to a local file system before the file is opened.

<?php
          ...
          if ($file = fopen($real_directory . '/' . $content, "r")) {
            $line_nr = 0;
            while (!feof($file)) {
              $line_nr++;
              $line = rtrim(fgets($file));
              // Get some fields from $line using substr().
            }
            fclose($file);
          }

Once every about 300K lines a line is not completely read into $line, and the substr() fails to get the data I expect: at a specific location in each line there's either the keyword 'IMPORTED' or 'REJECTED'. If that happens the code stops (by intention) as something would be wrong with the input file. Afterwards when I look at the "corrupted" file, the file is completely normal: no escape characters, nothing out of the ordinary. When I reprocess the file unchanged, it goes through without complaining (finding for the line with the latter problem the proper keyword).

I see it happening (although seldom), and I can't replicate it consistently.

PHP Version

PHP 8.0.26

Operating System

Debian GNU/Linux 11 (bullseye)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions