php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22124 succesive fgets() and fwrite() calls
Submitted: 2003-02-08 13:36 UTC Modified: 2003-02-08 14:26 UTC
From: rath_rat at hotmail dot com Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 4.3.1-dev OS: Windows XP Pro / UNIX
Private report: No CVE-ID: None
 [2003-02-08 13:36 UTC] rath_rat at hotmail dot com
Now here is a script that i've written to show my bug: (sorry it's so long, but this way the output is clear :)

<?
$tempfile="temp.txt";

if(!$fp= fopen($tempfile, "r+")) {
	echo "Opening file failed.\n";
} else {

	echo "\nReading original file..\n";
	$line=fgets($fp, 4096);
	echo "reading $tempfile: \"$line\"..\n";
	$line2=fgets($fp, 4096);
	echo "reading $tempfile: \"$line2\"..\n";
	
	echo "\nWorking with \$line2\n";
	$line2=explode(" ", $line2);
	$line2[1]++;
	$line2=implode(" ", $line2);
	
	rewind($fp);		
	echo "\nRewinding $fp..\n";
	
	$line = fgets($fp, 4096);
	echo "skipping the first line..\n";
	fwrite($fp, $line2);
	echo "writing $tempfile: \"$line2\"..\n";
	
	rewind($fp);
	echo "\nRewinding $fp..\n";
	
	echo "\nOutputing file again..\n";
	$line=fgets($fp, 4096);
	echo "reading $tempfile: \"$line\"..\n";
	$line2=fgets($fp, 4096);
	echo "reading $tempfile: \"$line2\"..\n";
	
	fclose($fp);
}
?>

temp.txt would look like this:
a 1
c 5

without a trailing \n after the 5.

Now what should happen is that the 5 should be incremented to 6 and get written back to the file. Under *nix this works fine, but under a windows version (sofar all that i've tried) it does not, the file is unchanged. My home platform is the Zend Development Enviroment (2.6), which runs PHP 4.2.2 or higher, under Windows XP Professional. As i mentioned earlier, under my system the file will be unchanged, but if i upload the same thing to a UNIX server, it will perform as expected and change "c 5" to "c 6".

If there's no fgets() call before the fwrite(), it will work, however (although that defeats the purpose of PHP, i'd like to be able to randomly access a file :D ).

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-02-08 14:06 UTC] sniper@php.net
Does seem to work quite right in Linux either (using PHP 4.3.1-dev from today)

 [2003-02-08 14:20 UTC] rath_rat at hotmail dot com
lol! there you go! well i can tell you that it did work properly on a UNIX server, i've uploaded it to a Tripod server: http://members.lycos.co.uk/m0nk3y613/test.php (lol please excuse the ads). You can see the text file, http://members.lycos.co.uk/m0nk3y613/temp.txt . Also note that since it is working, so it saves the "c #" line each time, and therefore its one more each time not nessisarily 5 before and 6 after, although i guess you figured that out :).
 [2003-02-08 14:26 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

After reviewing your script I believe that your program is faulty, you are tryind to do something that should not be done. Hence the 'errors', which you are seeing.

The 'r+' mode is primary intended for append operations, where you may need to read the data already in the file. Since on systems such as Win32 openning a file with 'a' or 'a+' mode will not allow you to read the data already in the file. Since the start position will be the end of the opened file rather then the beginning.

If you want to modify the contents of the existing file, then 1st read it into memory, make the changes you wish to make and then write it back to disk.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 18:02:40 2024 UTC