4

I am running PHP Version 5.3.4 with Apache/2.2.17 on Windows 7 Ultimate 32bit (IIS disabled). I been looking at the fopen modes and am well aware of what mode does what, but i can't wrap my finger around why the double posting to the txt file with a single fwrite. I've even tried modes: w and c.

Now I don't care whether the new data coming in is being prepend or appended, just so long as its there in the file without truncating existing data.

The real question is, why does the "a" mode instead of just appending new data, it writes (duplicates) the new data to the file twice before it closes.

In php code:

$fh = "";
if(!($fh = fopen("list.txt","x")) // if file exists, just open for writing (prepend)
   $fh = fopen("list.txt","a"); // if not exist, open just for writing (append)

fwrite($fh,"Somthing\r\n"); // write to file (filehandle, "data")
fclose($fh); // close the file

Results:
Somthing
Somthing

SOLVED: Found the culprit: nothing was wrong with my coding. It was an html validator extension I used for Chromium under Ubuntu 10.04. This extension was apparently causing something similar to instant page reload.

robx
  • 3,093
  • 2
  • 26
  • 29
  • you can use the "c" mode, which acts as "w" but doesn't wipe out the file. the double something seems like you ran the script twice. – yoavmatchulsky Apr 29 '11 at 20:34
  • Actually "c" mode seems to be truncating and writing new stuff. Each time i put new data in and load the php page, the old data is wiped in place of the new data. – robx Apr 29 '11 at 20:51
  • INdeed, that was the problem. I never would have thought! – Andrei Zisu Dec 17 '11 at 14:43

2 Answers2

1

I appreciate that this is years old but this problem is still known to occur and there is another reason that may also cause it. I didn't have the extension used by @robx, disabled all my extensions and no change - still double entries. However, after reading this thread about it being a Chrome bug (https://bugs.chromium.org/p/chromium/issues/detail?id=64810) comment 16 suggests the issue is the favicon being prefetched. Not ideal but delete the favion icon link tag in the head of the document and it stops double entries.

garrettlynchirl
  • 790
  • 8
  • 23
1
$fh = fopen("list.txt","a");
fwrite($fh,"Somthing\n"); // write to file (filehandle, "data")
fclose($fh); // close the file

your logic was not correct.

that's how its done, i hope it works for you

Ibu
  • 42,752
  • 13
  • 76
  • 103
  • sorry i fixed the little mistake, i had fopen x in both – Ibu Apr 29 '11 at 20:36
  • I thought that the "x" mode will return false if the file exist? And it still double posts to the txt file. – robx Apr 29 '11 at 20:43
  • actually you don't need to do a file exist, just do a `$fh = fopen("list.txt","a");` if the file doesn't exist it will create it, and if it does it will simply append the new data to it. – Ibu Apr 29 '11 at 20:51
  • That was my initial approach was just using the "a" mode. I am baffled about the duplicate entry. If I output it to HTML, I only get one time, but in writing to file there is duplicate entries. – robx Apr 29 '11 at 20:53
  • because with your aproach your are opening a handle twice so you are appending or writing twice. simply write this and give me feed back `$fh = fopen("list.txt","a"); fwrite($fh,"Somthing\n"); // write to file (filehandle, "data") fclose($fh); // close the file ` – Ibu Apr 29 '11 at 21:11
  • even as simple as those lines i still get double post into the file. – robx Apr 29 '11 at 21:23
  • maybe you should look at some other part of your file, are you using redirects? or is there another condition for writing? provide more code maybe this is not the problem – Ibu Apr 29 '11 at 21:27
  • no loop or nothing. just plain old $fh = fopen("list.txt","a"); fwrite($fh,"abc\r\n"); fclose($fh); @ http://robx.dyndns.info/app/fwrite.php all the links do is load the txt file and wipes content from the file. This page has no other codes, not even html. Just a straight php file with those simple few lines. Well, other than the links there's no other. I could strip everything but those, and you will see it double append every time you load the page. – robx Apr 29 '11 at 21:29
  • 1
    um have you checked the link you just gave? it works! you probably have a problem with your browser reloading or something. cause every time i reload the page i have one more line appended, isn't that what you are looking for? – Ibu Apr 29 '11 at 21:55
  • Weird. I am using Chromium on Ubuntu. Wow, i think its actually a chrome problem. Maybe the browser is reloading itself for whatever reason. Tried it from smart device and it works just as well. thanks. What a headache. – robx Apr 29 '11 at 21:56