4

我在 Windows 7 Ultimate 32 位(禁用 IIS)上运行 PHP 版本 5.3.4 和 Apache/2.2.17。我一直在研究fopen模式,并且很清楚什么模式可以做什么,但是我无法理解为什么使用单个 fwrite 双重发布到 txt 文件。我什至尝试过模式:w 和 c。

现在我不在乎传入的新数据是前置还是附加,只要它在文件中而不截断现有数据即可。

真正的问题是,为什么“a”模式不只是追加新数据,而是在关闭之前将新数据写入(复制)两次文件。

在 php 代码中:

$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

结果:

事某事

已解决:找到罪魁祸首:我的编码没有任何问题。这是我在 Ubuntu 10.04 下用于 Chromium的html 验证器扩展。这个扩展显然导致了类似于即时页面重新加载的事情。

4

2 回答 2

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

你的逻辑不正确。

这就是它的完成方式,我希望它对你有用

于 2011-04-29T20:34:06.327 回答
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.

于 2018-08-10T12:23:57.870 回答