2011-11-02 45 views
0

快速免責聲明:這是我的第二個PHP項目和第一個使用的腳本語言。PHP fwrite故障排除:覆蓋整數計數器

這個小小的網站是實施櫃檯的門戶。事情是,它運行,但不能正確增加。文件count.txt保留最初的0值(在第一行),並將第一行連續地加1(每次運行時,如「1111」)。每次產生的回聲是「當前計數:1」。

我見過的一些教程,fopen('r')原始添加/回顯,然後fopen('w')再次保存。我試過了,這絕對有效。我只是在探索是否可以鞏固。任何洞察爲什麼它跳到第二行或建議一般都非常感激。

<?php 
$countfile = "count.txt"; 
$counthandle = fopen($countfile, "r+"); 
$count = intval(@fread($counthandle, filesize($countfile))); 
$count++; 
echo "Current count: "; 
echo $count . "<br/>\n"; 
fwrite($counthandle, $count); 
fclose($counthandle); 
?> 
+0

寫(我想用'fseek')前應徵詢文件開頭。 –

回答

0

嘗試使用file_get_contentsfile_put_contents

<?php 
$countfile = 'count.txt'; 
$count = intval(file_get_contents($countfile)); 
$count++; 
printf ("Current count:<br />%s", $count); 
file_put_contents($countfile, $count); 
?>