file_put_contents 在 php 中自动递增重复的 int 值?

问题描述 投票:0回答:2

我有小代码。

total_request.txt 是文本文件,内容值为 =1

$file = 'total_request.txt';
$count = file_get_contents( $file );
echo $a = $count + 1;  
file_put_contents( $file, $a ); 

代码输出值:2

但是文件total_request.txt有内容3。我想在运行这个文件php时自动递增1。

我的代码有什么问题?

php file-put-contents
2个回答
0
投票

您可能希望以追加模式打开文件:

<?php
$fp = fopen('data.txt', 'a');//opens file in append mode  
fwrite($fp, ' this is additional text ');  
fwrite($fp, 'appending data');  
fclose($fp);
?>

希望对你有帮助。

问候。


0
投票

我花了 1 天的时间才弄清楚到底发生了什么。 我检查了网络选项卡,发现每次重新加载页面时,它都会被视为一个新请求 把它放在这里也许可以节省你的时间:)

© www.soinside.com 2019 - 2024. All rights reserved.