i have this code which is supposed to increment a counter on page

<?php  
 

//create a file called counter.txt and upload it to your server
//now open the file
$fp = fopen("counter.txt" , "r");
//read in the current count
$count = (int)fread($fp, 1024);
//increment the count by 1
$count++;
//close the file
fclose($fp);
//image display, get the lentgth of the count





echo"<table cellspacing=0 width =4% cellpadding=0 row=1 column=".strlen($count);
echo"<tr>";
for ($i = 0 ;$i < strlen($count) ; $i++)
{
$imgsrc = substr($count,$i ,1);
//display the image(s) note our images are in a folder located at
//images/count1/ change this to your location
echo"<td width=2%>";
echo "<img src =\"images/count1/" . $imgsrc . ".gif\">";
echo"</td>";
}
echo"</tr>";
echo"</table>";


//open the counter file
$fp = fopen("counter.txt", "w");
//write the new count to the file
fwrite($fp, $count);
//close the file
fclose($fp);
//include("grcounter.php"); 

 ?>

it is reading from text file and display the value from file but it is failing to update the file with the new value which is an increment of the counter

Dani AI

Generated

The symptom described by (current value prints but the file never changes) is almost always environmental rather than a logic bug in the display loop. 's reply gets you closer, but the usual culprits are: the web process lacks write permission on the file, the script is using a relative path the server resolves differently, writes are failing silently, or concurrent requests overwrite each other. Before changing display code, verify the process can actually write to the counter file.

A simple, robust pattern that avoids explicit fopen/fwrite sequence and uses an atomic write is to read the file, cast to int, increment, then use a single write call that requests an exclusive lock. Use an absolute path (dirname(FILE) or similar) and check the write result so failures get logged:

<?php
$path = dirname(__FILE__) . '/counter.txt';
$contents = @file_get_contents($path);
$count = (int) $contents;
$count++;
$result = @file_put_contents($path, (string)$count, LOCK_EX);
if ($result === false) {
    error_log("Failed writing counter to $path");
}
?>

If that still fails, run this checklist: confirm the file exists and initialize it to 0; confirm ownership/permissions (on Linux the web user is often www-data or apache) and temporarily set writable permissions for testing; check server error logs and PHP error_reporting; watch for open_basedir, safe_mode or SELinux/AppArmor restrictions. For higher traffic or reliability, move the counter to a DB and use an atomic increment query, or a memory store (Redis) to avoid race conditions. For reference, PHP docs for file_put_contents and locking via flock explain the relevant flags and behaviors.

try this:

<?php
$open = fopen('counter.txt', 'r');
$read = fread($open, filesize('counter.txt'));
fclose($open);
$count = $read + 1;
$open = fopen('counter.txt', 'w');
$write = fwrite($open, $count);
fclose($open);

echo"<table cellspacing=0 width =4% cellpadding=0 row=1 column=".strlen($count);
echo"<tr>";
for ($i = 0 ;$i < strlen($count) ; $i++)
{
$imgsrc = substr($read,$i ,1);
//display the image(s) note our images are in a folder located at
//images/count1/ change this to your location
echo"<td width=2%>";
echo "<img src =\"images/count1/" . $imgsrc . ".gif\">";
echo"</td>";
}
echo"</tr>";
echo"</table>";
?>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.