jjmusicpro Posted June 8, 2008 Share Posted June 8, 2008 i am having trouble with this... i wanted to open a text file c.txt, get the number thats in there, add to it, and close the file here is what i have <?php $file = fopen("c.txt", "r") or exit("Unable to open file!"); //Output a line of the file until the end is reached while(!feof($file)) { echo "NUMBER -".fgets($file). "<br />"; } fclose($file); ?> Quote Link to comment https://forums.phpfreaks.com/topic/109279-solved-open-a-text-file-get-number-add-to-it-and-close-it/ Share on other sites More sharing options...
wildteen88 Posted June 8, 2008 Share Posted June 8, 2008 If the file only contains just a number use file_get_contents. $number = trim(file_get_contents('c.txt')); echo 'NUMBER: ' . $number; Quote Link to comment https://forums.phpfreaks.com/topic/109279-solved-open-a-text-file-get-number-add-to-it-and-close-it/#findComment-560541 Share on other sites More sharing options...
keeB Posted June 8, 2008 Share Posted June 8, 2008 <?php $file = fopen("c.txt", "r") or exit("Unable to open file!"); //Output a line of the file until the end is reached while(!feof($file)) { $num = fgets($file); } fclose($file); $file = fopen("c.txt", "w"); fwrite($file, $num+1); fclose($file); ?> Quote Link to comment https://forums.phpfreaks.com/topic/109279-solved-open-a-text-file-get-number-add-to-it-and-close-it/#findComment-560542 Share on other sites More sharing options...
jjmusicpro Posted June 8, 2008 Author Share Posted June 8, 2008 If the file only contains just a number use file_get_contents. $number = trim(file_get_contents('c.txt')); echo 'NUMBER: ' . $number; dont i have to put something to update the number and write it back to the file, so next time someone comes it will +? Quote Link to comment https://forums.phpfreaks.com/topic/109279-solved-open-a-text-file-get-number-add-to-it-and-close-it/#findComment-560543 Share on other sites More sharing options...
wildteen88 Posted June 8, 2008 Share Posted June 8, 2008 Yes use file_put_contents $number = trim(file_get_contents('c.txt')); echo 'NUMBER: ' . $number; $new_number = $number + 6; // whatever you want to add to the number file_put_contents('c.txt', $new_number); Quote Link to comment https://forums.phpfreaks.com/topic/109279-solved-open-a-text-file-get-number-add-to-it-and-close-it/#findComment-560546 Share on other sites More sharing options...
jjmusicpro Posted June 8, 2008 Author Share Posted June 8, 2008 Yes use file_put_contents $number = trim(file_get_contents('c.txt')); echo 'NUMBER: ' . $number; $new_number = $number + 6; // whatever you want to add to the number file_put_contents('c.txt', $new_number); Perfect that worked! Question, is this a good way to track hits? or should i do it in a DB? Quote Link to comment https://forums.phpfreaks.com/topic/109279-solved-open-a-text-file-get-number-add-to-it-and-close-it/#findComment-560549 Share on other sites More sharing options...
keeB Posted June 8, 2008 Share Posted June 8, 2008 You'd have a rough time with concurrency in a flat text file. Quote Link to comment https://forums.phpfreaks.com/topic/109279-solved-open-a-text-file-get-number-add-to-it-and-close-it/#findComment-560562 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.