tags:

views:

52

answers:

3
$dirname1 = '../counters'; 
$dirN = '../uploads';
$extens = ".txt";
$fh = fopen(".$dirname1/$filename.$extens", "w");



if(fopen(".$dirN/$filename.", "r")) {
     $count_my_page = (".$dirname1/$filename.$extens");
     $hits = file($count_my_page);
     $hits[0] ++;
     $fp = fopen($count_my_page , "w");
     fputs($fp , "$hits[0]");
     fclose($fp);
     echo $hits[0];
}

each time i open the file, the count should be updated.... but it happens only for the first time.....

A: 

As count you mean the $count_my_page var ? It is declared inside the if scope, so every loop it is created as a new one. try declare it outside.

kajyr
$count_my_page is a file name, it's a first line within if statement.
SilentGhost
A: 

Is that all in the same file? If so, your first fopen with "w" mode will truncate the file before you read it.

Jonathan Maddison
he reads it with file first
SilentGhost
I mean the fopen() on line 4. It seems to be before any reading?
Jonathan Maddison
+1  A: 

Think it might be to do with the increment operator (++), try this:

$hits = file($count_my_page);
$nHits = ((int) $hits[0]) + 1;
$fp = fopen($count_my_page , "w");
fputs($fp , $nHits . "");
fclose($fp);
echo $nHits;
karim79
This works, but while refreshing the page.... not on clicking or reading the file.....
@theband - I don't understand, could you please elaborate on that a bit?
karim79
if i refresh the page where the link is there, it increase the count... not on reading the file.