php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #11855 fgets returns truncated data
Submitted: 2001-07-03 10:12 UTC Modified: 2001-08-19 03:45 UTC
From: wiersig at glamus dot de Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 4.0.5 OS: Linux 2.2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: wiersig at glamus dot de
New email:
PHP Version: OS:

 

 [2001-07-03 10:12 UTC] wiersig at glamus dot de
while reading chunks using fgets from a pdf-File, it occured that fgets seems to read data until a 0x0a occurs but returned the data until the first occurence of 0x00 only. Because of that, half of the data was lost.

example

Hello0x0d0x0a
XYZ

will be read an returned correctly, the data containing "Hello" and the filepointer on the XYZ

He0x00llo0x0d0x0a
XYZ

will retrurn data containing "He" with the filepointer still on the XYZ

my code:
        $fp=fopen($pdf_file,"r");
        while (!feof($fp)) {
            $pdf_line="|".strtoupper(fgets($fp,4096));
            $filepos+=strlen($pdf_line)-1;
            $i=0;
            while ($i=strpos($pdf_line,"Needle",$i)) {
                 if ($i<4000) {
                     $i+=strlen("Needle");
                     $retval[]=substr($pdf_line,$i,strlen("Needle"));
                 } else {
                     $i++;
                }
            }
            if (strlen($pdf_line)>4000) {
                fseek($fp,$filepos-100);
                $filepos-=100;
            }
        }
        fclose($fp);

we solved the problem by using fread() instead of fgets(), but felt you still might be interested.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-08-19 03:45 UTC] sniper@php.net
Yes, fread() is binary safe, fgets() isn't. 
Not a bug.

--Jani

 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC