php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #15117 fopen and fwrite writes to much data in mode a and a+
Submitted: 2002-01-19 17:07 UTC Modified: 2002-08-11 01:00 UTC
Votes:4
Avg. Score:4.0 ± 1.2
Reproduced:4 of 4 (100.0%)
Same Version:2 (50.0%)
Same OS:2 (50.0%)
From: bitbybit at fibre dot a2000 dot nl Assigned:
Status: No Feedback Package: Filesystem function related
PHP Version: 4.1.1 OS: W2K
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
35 - 25 = ?
Subscribe to this entry?

 
 [2002-01-19 17:07 UTC] bitbybit at fibre dot a2000 dot nl
---- code ----
$orderstring = $date."\t".	//current time
	$tireqty." tires\t".	//received form field
	$oilqty." oil\t".	//received form field
	$sparkqty." sparks\t".	//received form field
	$totalamount."\t".	/calc. from form fields
	$address."\n";		//received form field

$fp = fopen("orders.txt", "a");
fwrite($fp, $orderstring);
fclose($fp);

---- output in orders.txt ----
22:32 19th Saturday 2002	4 tires	1 oil	12 sparks	503.80	NL
22:32 19th Saturday 2002	 tires	 oil	 sparks	0.00	
Bug:
When using fopen() in mode a or a+ the last line will be added without any obvious cause.
When fopen() is used in same code but in mode w it works correct like expected.
I haven't tested fopen() in all other available modes.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-01-20 04:16 UTC] sander@php.net
Are you sure the file is empty before the fopen() call?
See www.php.net/fopen for some more info...
 [2002-01-20 06:26 UTC] bitbybit at fibre dot a2000 dot nl
It doesn't really matter with fopen() in append mode. It just opens the file to append new data to the end of the file. What data will be added, depents on when(?) and what(?)data the user submitted with the form. Till that point it goes fine. But then there is this string with same time mark being added to the file. Ofcourse multiple submitions result in this same pattern being repeated over and over again.
 [2002-01-26 00:10 UTC] webmaster at lxtype dot com
Reproduced it. However, after the first two times, the same code stopped doing it (I only changed the string?). Seems to be semi-random at least, or something. Could it be a browser bug, having the page "accidentally" being submitted/reloaded?

Temporary "fix" is to conditionally open it:

if(!file_exists('orders.txt') || @file_size('orders.txt')==0)
	$fp = fopen('orders.txt', 'w');
else
	$fp = fopen('orders.txt', 'a');
 [2002-07-10 23:50 UTC] sniper@php.net
Please try this snapshot:

http://snaps.php.net/win32/php4-win32-latest.zip

 [2002-08-11 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2007-07-30 13:19 UTC] WGFTARE at RET dot DE
Hi,

'a+' Mode always insert my string two times instead of one time. It really sucks (i have to use php4 here) and this bug is so annyoing, FIX IT!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 06:01:29 2024 UTC