Jump to content

Read and write a file counter


guarana

Recommended Posts

hi all.

 

i have this simple script: if you see it in the browser, he should increase the number in text file, but this doesn't work.

 

<?php
include("count.txt");
$file=fopen("count.txt","r+");
$num=fgets($file,20);
fseek($file, 0);
$num++;
fputs($file,$num);
fclose($file);
?>

 

I do not get no mistake: simply, he does not increase the value

Where it is wrong?

Link to comment
https://forums.phpfreaks.com/topic/73105-read-and-write-a-file-counter/
Share on other sites

Here is a copy of my class for the survey script (which allows me to tally votes, this is where I'm getting the code from)

 

Basically, here's the simple set-up

 

This allows me to get all contents of the file, if there's more than one line (I'm improving on this code so it just searches for the word "Tally" and uses that line instead of specifying.

		$file_in_question = "your file";
		$file = @fopen($file_in_question,"r") or die("Can't open $file_in_question. It either doesn't exist or there's an internal server error.");
		while(!feof($file))
		{
			$file_data_array[$i] = fgets($file);
			$i++;
		}
		fclose($file);

$file_data_array is an array containing all the lines in the text file

 

In your case, $file_data_array[0] contains the number. So next... we do this

		//Handle the last line, split it up, add up the corresponding value, put it back into a line
		$curr_file_stats = $file_data_array[0]++;

		//Join it with a \n for the new lines ;-) This works, don't know why.
		$text_to_add = join("",$curr_file_stats);

		//Simply write this data back into our file... So we open, overwrite, and close

		$file = @fopen($file_in_question,"w") or die("Can't open $file_in_question. It either doesn't exist or there's an internal server error.");
		@fwrite($file,$text_to_add);
		fclose($file);

uhm... Maybe I have something wrong but a code as this doesn't work:

 

<?php
//show the number
include("myfile.txt");

		$file_in_question = "myfile.txt";
		$file = fopen($file_in_question,"r") or die("Can't open $file_in_question....");
		while(!feof($file))
		{
			$file_data_array[$i] = fgets($file);
			$i++;
		}
		fclose($file);


//Handle the last line, split it up, add up the corresponding value, put it back into a line
		$curr_file_stats = $file_data_array[0]++;

		//Join it with a \n for the new lines ;-) This works, don't know why.
		$text_to_add = join("",$curr_file_stats);

		//Simply write this data back into our file... So we open, overwrite, and close

		$file = fopen($file_in_question,"w") or die("Can't open $file_in_question....");
		fwrite($file,$text_to_add);
		fclose($file);
?>

 

the errors are:

 

Notice: Undefined variable: i in write.php on line 22

 

Notice: Undefined variable: i in write.php on line 23

 

Notice: Undefined offset: 0 in write.php on line 29

 

Warning: join() [function.join]: Bad arguments. in write.php on line 32

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.