Trying to count, but changes to 1
<p>I am trying to make a counter. What I mean by that is a button that uses a XHTMLrequest and just runs this PHP.</p>

<p>My question is why is my counting code changing the value of the text document to the number <code>1</code>. If I just change the value to for example <code>24</code>, instead of adding 1 and changing the value to <code>25</code>, it changes the value to the number <code>1</code>.</p>

<pre><code>&lt;?php

$fp = false;
// Open file for reading, then writing
while ( ($fp=fopen('clicks.txt','r+'))===false ) {
    usleep(250000); // Delay 1/4 second
}
// Obtain lock
while ( !flock($fp, LOCK_EX) ) {    
    usleep(250000); // Delay 1/4 second
}
// Read Clicks
$clicks = trim(fread($fp,1024));
// Add click
$clicks++;
// Empty file
ftruncate($fp,0);
// Write clicks
fwrite($fp, $clicks);
// Release Lock
flock($fp, LOCK_UN);
// Release handle
fclose($fp);

?&gt;
</code></pre>
