gpt4 book ai didi

php-5.3 - 为什么 PHP fopen + fwrite 会出现双文档?

转载 作者:行者123 更新时间:2023-12-02 17:31:39 25 4
gpt4 key购买 nike

我正在 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
<小时/>

结果:
东西
东西

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

最佳答案

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

你的逻辑不正确。

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

关于php-5.3 - 为什么 PHP fopen + fwrite 会出现双文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5837063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com