I wrotet this little function that essentially justs writes my counter to a file. For some reason, if I reset the file to an empty file, it increments but only to 1 (then it stops incrementing).
I put print statements all over the place and it actually thinks it incremented but it didn't. I'm guessing it has something to do with the way i'm reading the file (it always reads that the file is empty and increments hence always results in 1).
Can someone help me out here?
function updateCounter()
{
// Check if the file exists
if(!file_exists("counter.txt"))
print "FILE doesn't exists";
//Update counter
$counterFile = fopen("counter.txt", 'rw');
if($counterFile)
{
$chunk = fread($counterFile, 10); // Get the last counter value
fclose($counterFile); // Close the file to rewind the pointer
$counterFile = fopen("counter.txt", 'rw');
if($counterFile)
{
//print "CHUNK: $chunk<BR>";
$chunk = $chunk + 1; // Increment counter
fwrite($counterFile, $chunk); // Write the new value
}
else
{
print "Did not update chunk value";
}
// print "Just changed the counter<BR>";
return 0;
}
else
{
// print "failed to change the counter<BR>"; // It failed
return -1;
}
fclose($counterFile); // Close the counter file
}