php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51640 Fwrite writes twice a text
Submitted: 2010-04-23 01:57 UTC Modified: 2010-06-10 23:53 UTC
From: spiderboy1989 at gmail dot com Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 5.3.2 OS: Windows XP
Private report: No CVE-ID: None
 [2010-04-23 01:57 UTC] spiderboy1989 at gmail dot com
Description:
------------
I'm having troubles with fwrite() function. It is writing twice a text I just need to write once. I searched in google for the same problem, and I found the same bug reported here : http://bugs.php.net/bug.php?id=21916, and here : http://bugs.php.net/bug.php?id=16225, but neither of both was solved. 

This is how it writes into the file :

"First

Second

Hello world!
Hello world!
Third"

As you can see, the string "hello world!" is writed twice, and that should not happen. Also, and this is weird, the line fwrite($op, $lastLine); prints the text correctly, just once...


Test script:
---------------
$file = "test.txt";
$string = "Hello world!\r\n";
$op = fopen($file,"r+");
$exp = explode("\n", fread($op, filesize($file)));
$lastLine = end($exp);
fseek($op, -strlen($lastLine), SEEK_END);
fwrite($op, $string);
fwrite($op, $lastLine);
fclose($op);

Expected result:
----------------
The text should be writed just once.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-01 10:19 UTC] g_i_ivanov at mail dot bg
I have the same problem: 

Windows XP Professional SP3, Apache/2.2.14, PHP 5.2.13 (cli)

code:
------------------------------------------
$fp = fopen("d:/Projects/test/logs/testfile.txt", "a");
fwrite($fp, date('c') . " - Sample text" . PHP_EOL);
fclose($fp);

result: 
------------------------------------------
2010-05-31T10:36:37+03:00 - Sample text
2010-05-31T10:36:37+03:00 - Sample text
 [2010-06-08 14:09 UTC] tony2001@php.net
-Status: Open +Status: Bogus
 [2010-06-08 14:09 UTC] tony2001@php.net
Not reproducible.
Try providing a working example script, the one in the report doesn't work.
 [2010-06-10 23:53 UTC] spiderboy1989 at gmail dot com
No way... i tested again, the same code, the same code that was giving me troubles... I tested the g_i_ivanov's code, and both are working! This must be a joke... I didn't change php version, I didn't change the code... This can't be true :/...

I'm sorry for wasting your time. If I see any weird behavior, I will let you know it.
 [2018-10-07 23:43 UTC] bluegman991 at gmail dot com
I know i'm a few years late lol. But this is for anyone who still has come across this issue.

I ran across this issue today. Everything I did confirmed fwrite was only happening once, but yet somehow, the data in the file said it was happening twice.

Then I thought maybe the script is being called twice. So I manually called the script via the command line, and it only happened once. However, when I access the script by loading the webpage, it happens twice.

Skipping to the end, I dumped the _SERVER contents, and found the second write came from a second request browsers automatically make to /favicon.ico

The issue happened because I and as some others may have a fallback/ReWrite setup, so any non-existent script request are re-routed to another file like index.php
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 16:01:29 2024 UTC