guarana Posted October 13, 2007 Share Posted October 13, 2007 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? Quote Link to comment Share on other sites More sharing options...
marcus Posted October 13, 2007 Share Posted October 13, 2007 Does the file have the correct permissions to be written? Quote Link to comment Share on other sites More sharing options...
guarana Posted October 13, 2007 Author Share Posted October 13, 2007 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! Quote Link to comment Share on other sites More sharing options...
kratsg Posted October 13, 2007 Share Posted October 13, 2007 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); Quote Link to comment Share on other sites More sharing options...
guarana Posted October 14, 2007 Author Share Posted October 14, 2007 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 Quote Link to comment Share on other sites More sharing options...
harristweed Posted October 14, 2007 Share Posted October 14, 2007 I have tried your original code on my computer and it works as it should, therefore the problem is elsewhere. Sorry can't help more. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.