php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22682 Proplems with fread(), ftell() and fseek() and CRLF newlines
Submitted: 2003-03-13 11:41 UTC Modified: 2003-08-17 18:34 UTC
Votes:6
Avg. Score:3.8 ± 1.5
Reproduced:3 of 4 (75.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: fbeyer at clickhand dot de Assigned:
Status: No Feedback Package: Filesystem function related
PHP Version: 4CVS-2003-04-28 (stable) OS: Windows XP
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: fbeyer at clickhand dot de
New email:
PHP Version: OS:

 

 [2003-03-13 11:41 UTC] fbeyer at clickhand dot de
I have found this bug in 4.3.0, searched matching bug reports and they said the bug is fixed in the latest stable CVS snap. I have downloaded and installed today's latest version, the fix is mentioned in the news.txt, but the bug is still there.

Here are my test files for a short reproducing script:

file test.txt:
(saved in dos format, ie. newlines are CRLF/\r\n):
------------------------------
This is a test file
in win32 / dos format
ie. lines end with CRLF
resp. \r\n
------------------------------

file test.php
------------------------------
<?php
$fp = fopen('test.txt', 'r');

$contents = fread($fp, 30);
fseek($fp, ftell($fp));
$contents .= fread($fp, 30);

echo $contents;

echo "\n\n";

var_dump(strpos($contents, "\r"));
?>
------------------------------

Output:
------------------------------
This is a test file
in win32 // dos format
ie. lines end wit

bool(false)
------------------------------

It can be seen that fseek() or ftell() don't work as expected: the '/' character is read twice.

Furthermore, fread() doesn't return \r characters. I also have tested this for fgetc() - \r is not read.

A strange thing is that this bug doesn't occurr in the newest php5/ze2 snapshot.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-04-28 09:18 UTC] fbeyer at clickhand dot de
No, unfortunately, the result is the same.

But I've found out that the problem only occurs if PHP is run as an apache module (I'm currently using 1.3.20). The CGI executable works as expected - this is why I mentioned that the bug doesn't occur in the PHP5 snapshot since I've installed it using CGI.
 [2003-04-28 12:31 UTC] wez@php.net
Related to Bug #22497
 [2003-05-11 08:28 UTC] moriyoshi@php.net
As Wez pointed out above, this is a generic __setmode() issue.
status: open => verified

 [2003-08-17 18:34 UTC] sniper@php.net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.


 [2003-11-28 15:32 UTC] alexey at ozerov dot de
I still have a related bug on Windows XP using PHP 4.3.4. Sample:

<?
header("Content-type: text/plain");
ini_set("auto_detect_line_endings", true); // Doesn't matter
$file=fopen(__FILE__,"rt");                // "t" doesn't matter too

$linenumber=0;

while ($file && $line=fgets($file, 65536))
{ echo ("$linenumber: ".$line."\n");
  fseek($file, ftell($file), SEEK_SET);
  $linenumber++;
}
fclose($file);
?>

fseek() should not do anything but it _shifts_ the file pointer (as many bytes back as many "\r" are in the last read input) if I the __FILE__ has DOS encoded line breaks. As the result every line is followed by a blank line in the sample output.
 [2004-04-27 00:45 UTC] frank at heuveltop dot nl
I run PHP as a Apache (2.0.48) module. I tried several methods but there is no way PHP will read "\r" from the file. The filepointer returned by ftell is getting out of sync. I do confirm all the symptoms that are stated above. Allthought I can build a workaround by counting lines and add the number of lines to ftell, I am worried about reading binary files like images by GD library.
 [2008-07-25 09:22 UTC] nicolas dot castagne at imag dot fr
I don't use Python, but found this bug report and can add a comment.

Windows c library has a bug with ftell.
See :
http://mail.python.org/pipermail/python-bugs-list/2006-January/031684.html
http://www.cygwin.com/faq/faq.api.html
http://sourceforge.net/tracker/index.php?func=detail&aid=1381717&group_id=5470&atid=105470
http://gnuwin32.sourceforge.net/compile.html
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC