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
Share on other sites

yes, rw-rw-rw or rw-r-rw.... but the number in .txt file is not increased.

 

One thing: every time I call this page in the browser having the count.txt file opened in editor, when i reopen the .txt file, the editor say that this file is changed!

 

Link to comment
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);

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.