witakr Posted May 7, 2007 Share Posted May 7, 2007 Hello, I am trying to get the contents of a txt file and add a number to it and then store the sum of that equation in the file such as: file.txt = 300 File contents: 300 Add this to it: 150 ----------------------- Stored this : 450 and now file.txt = 450 here is what i am using... <body> <?php $filesdir = "playerfiles/"; $sitepath = "http://www.*****.com/"; $scriptdir = "scorekeeper/"; $filesdir = "playerfiles/"; $addpoints = $_POST["addpoints"]; $playername = $_POST["playername"]; $pnlower = strtolower($playername); $playerfile = $pnlower.".txt"; if( !$playername ) { echo "You need to enter a playername<br><a href='index.php'>[ Go Back ]</a>"; exit; } if( $addpoints <= 0 or !$addpoints ) { echo "You need to enter a points value that is greater than zero.<br><a href='index.php'>[ Go Back ]</a>"; exit; } $ps = fopen("playerfiles/$playerfile", "w+"); if(!$ps){ echo "<BR><BR><B>Unable to write to file(".$playerfile."), Contact system administrator</b>"; } else { $filecon = file_get_contents("playerfiles/$playerfile"); $totalpoints = "$filecon + $addpoints"; $scorewrite = "$totalpoints"; fwrite($ps, "$scorewrite"); fclose($ps); } if ($ps) { //echo $filecon; echo $playername."<br>"; echo $addpoints." + ".$filecon." = ".$totalpoints."<br>"; echo "File name: ".$playerfile; } else { echo "<BR><BR><B>Unable to write to file(".$playerfile."), Contact system administrator</b>"; exit; } ?> </body> Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/ Share on other sites More sharing options...
jitesh Posted May 7, 2007 Share Posted May 7, 2007 <?php // get contents of a file into a string $filename = "file.txt"; $handle = fopen($filename, "w+"); $old_contents = fread($handle, filesize($filename)); fwrite($handle, $old_contents + 100); fclose($handle); ?> Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-246995 Share on other sites More sharing options...
witakr Posted May 7, 2007 Author Share Posted May 7, 2007 OK, thats great, thanks... now I incorperated that into my code: <?php $filesdir = "playerfiles/"; $sitepath = "http://www.*****.com/"; $scriptdir = "scorekeeper/"; $filesdir = "playerfiles/"; $addpoints = $_POST["addpoints"]; $playername = $_POST["playername"]; $pnlower = strtolower($playername); $playerfile = $pnlower.".txt"; if( !$playername ) { echo "You need to enter a playername<br><a href='index.php'>[ Go Back ]</a>"; exit; } if( $addpoints <= 0 or !$addpoints ) { echo "You need to enter a points value that is greater than zero.<br><a href='index.php'>[ Go Back ]</a>"; exit; } $ps = fopen("playerfiles/$playerfile", "w+"); if(!$ps){ echo "<BR><BR><B>Unable to write to file(".$playerfile."), Contact system administrator</b>"; } else { $filename = "$filesdir$playerfile"; $old_contents = fread($ps, filesize($filename)); //this is line 37************************************ fwrite($ps, $old_contents + $addpoints); fclose($ps); } if ($ps) { //echo $filecon; echo $playername."<br>"; echo $addpoints." + ".$filecon." = ".$totalpoints."<br>"; echo "File name: ".$playerfile; } else { echo "<BR><BR><B>Unable to write to file(".$playerfile."), Contact system administrator</b>"; exit; } ?> but it produces this error: Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/witakr/public_html/scorekeeper/processpoints.php on line 37 Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-247014 Share on other sites More sharing options...
genericnumber1 Posted May 7, 2007 Share Posted May 7, 2007 you know there is a function file_get_contents()... http://www.php.net/file_get_contents Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-247017 Share on other sites More sharing options...
witakr Posted May 7, 2007 Author Share Posted May 7, 2007 if you look at my 1st post you will see in the code that i was using that func at first.... Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-247018 Share on other sites More sharing options...
witakr Posted May 7, 2007 Author Share Posted May 7, 2007 ok, modified a little more using file_get_contents() $ps = fopen("playerfiles/$playerfile", "w+"); if(!$ps){ echo "<BR><BR><B>Unable to write to file(".$playerfile."), Contact system administrator</b>"; } else { if ($ps) { echo $addpoints; $filename = "$playerfile"; $old_contents = file_get_contents("playerfiles/".$filename); fwrite($ps, $old_contents + $addpoints); fclose($ps); $totalpoints = $addpoints + $old_contents; echo $playername."<br>"; echo $addpoints." + ".$old_contents." = ".$totalpoints."<br>"; echo "File name: ".$playerfile; } } this produces this output when we try to add 250 to the file contents: 250name 250 + = 250 File name: name.txt Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-247022 Share on other sites More sharing options...
genericnumber1 Posted May 7, 2007 Share Posted May 7, 2007 if you look at my 1st post you will see in the code that i was using that func at first.... ah, sorry Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-247025 Share on other sites More sharing options...
jitesh Posted May 7, 2007 Share Posted May 7, 2007 $old_contents = fread($ps, filesize("playerfiles/$playerfile")); // Line 37 Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-247122 Share on other sites More sharing options...
witakr Posted May 7, 2007 Author Share Posted May 7, 2007 alrighty... i tried that and i got an error here is my code: $ps = fopen("playerfiles/$playerfile", "w+"); if(!$ps){ echo "<BR><BR><B>Unable to write to file(".$playerfile."), Contact system administrator</b>"; } else { if ($ps) { echo $addpoints; $filename = "$playerfile"; $old_contents = fread($ps, filesize("playerfiles/$playerfile")); // Line 37 fwrite($ps, $old_contents + $addpoints); fclose($ps); $totalpoints = $addpoints + $old_contents; echo $playername."<br>"; echo $addpoints." + ".$old_contents." = ".$totalpoints."<br>"; echo "File name: ".$playerfile; } } here is the error/output: 250 Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/witakr/public_html/scorekeeper/processpoints.php on line 38 name 250 + = 250 File name: name.txt ...this is with an input of 250 again Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-247244 Share on other sites More sharing options...
witakr Posted May 7, 2007 Author Share Posted May 7, 2007 ok, I changed it a little more and it does not produce an error but its not adding the new input to the existing value it will open the file, read the contents, CHANGE the contents and close file.. I need it to read file contents, ADD TO the contents, close file if( $addpoints <= 0 or !$addpoints ) { echo "You need to enter a points value that is greater than zero.<br><a href='index.php'>[ Go Back ]</a>"; exit; } $ps = fopen("playerfiles/$playerfile", "w+"); if(!$ps){ echo "<BR><BR><B>Unable to write to file(".$playerfile."), Contact system administrator</b>"; } else { if ($ps) { echo $addpoints; $filename = "$playerfile"; $old_contents = fpassthru($ps); // Line 37 fwrite($ps, $old_contents + $addpoints); fclose($ps); $totalpoints = $addpoints + $old_contents; echo $playername."<br>"; echo $addpoints." + ".$old_contents." = ".$totalpoints."<br>"; echo "File name: ".$playerfile; } } Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-247253 Share on other sites More sharing options...
igor berger Posted May 7, 2007 Share Posted May 7, 2007 I tried doing w+ but got the same error as you did on PHP 4.1 so I revised it a little..... <?php // get contents of a file into a string $filename = "test_score.txt"; $handle = fopen($filename, "r"); $old_score = fread($handle, filesize($filename)); fclose($handle); $add_score=5; $new_score=$old_score+$add_score; $handle = fopen($filename, "w"); fwrite($handle, $new_score); fclose($handle); echo $old_score."+".$add_score."=".$new_score; ?> see it work http://www.travelconnecxion.com/webmaster/test_score.php Dont forget to donate one cent for my hard work Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-247303 Share on other sites More sharing options...
igor berger Posted May 7, 2007 Share Posted May 7, 2007 One thing about using text files to store your data, that I do not like, is that you need to set CHMOD to 766 for the file. This opens you up to hackers that are on your shared host account! Read this articale http://www.totalchoicehosting.com/forums/index.php?showtopic=28009 I recommend to use MySql to store data, you need a user id and password to have access to it. VVery secure and easy to use! Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-247337 Share on other sites More sharing options...
witakr Posted May 7, 2007 Author Share Posted May 7, 2007 Yes, actually I agree with you. I just used text files here because they will only contain a number which is a scorfe in a contest. thats it... If I was going to store anything sensitive then i would pursue a sql db... thanks for the suggestion though Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-247465 Share on other sites More sharing options...
witakr Posted May 7, 2007 Author Share Posted May 7, 2007 ok, when i implemented it into my code: <?php $sitepath = "http://www.*****.com/"; $scriptdir = "scorekeeper/"; $filesdir = "playerfiles/"; $addpoints = $_POST["addpoints"]; $playername = $_POST["playername"]; $pnlower = strtolower($playername); $filename = $pnlower.".txt"; if( !$playername ) { echo "You need to enter a playername<br><a href='index.php'>[ Go Back ]</a>"; exit; } if( $addpoints <= 0 or !$addpoints ) { echo "You need to enter a points value that is greater than zero.<br><a href='index.php'>[ Go Back ]</a>"; exit; } //$ps = fopen("playerfiles/$playerfile", "w+"); //if(!$ps){ echo "<BR><BR><B>Unable to write to file(".$playerfile."), Contact system administrator</b>"; } // else { $handle = fopen("playerfiles/$filename", "r"); $old_score = fread($handle, filesize($filename)); fclose($handle); $add_score = $addpoints; $new_score = $old_score + $add_score; $handle = fopen("playerfiles/$filename", "w"); fwrite($handle, $new_score); fclose($handle); echo $old_score."+".$add_score."=".$new_score; ?> ... it produced this errror/output: Warning: filesize() [function.filesize]: stat failed for jason.txt in /home/witakr/public_html/scorekeeper/processpoints.php on line 32 Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/witakr/public_html/scorekeeper/processpoints.php on line 32 +154=154 Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-247589 Share on other sites More sharing options...
per1os Posted May 7, 2007 Share Posted May 7, 2007 <?php $old_score = fread($handle, filesize($scriptdir . $filename)); Remember that you have to point to the relative or absolute location of where the file is located. Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-247603 Share on other sites More sharing options...
igor berger Posted May 7, 2007 Share Posted May 7, 2007 is playerfiles/$filename in this dir? /home/witakr/public_html/scorekeeper/ if not do this /home/witakr/public_html/scorekeeper/playerfiles/$filename echo $filename before opening the file make sure it is the right file first. Check your CHMOD should be 766 Do you have data in your text file? Did you hit return when you inserted the number in the file? Maybe you have bad data in the file? Try this to get info! // outputs e.g. somefile.txt: 1024 bytes $filename = 'somefile.txt'; echo $filename . ': ' . filesize($filename) . ' bytes' Do not post to this script just add a number and file name on the script, you might be posting something bad. Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-247650 Share on other sites More sharing options...
witakr Posted May 8, 2007 Author Share Posted May 8, 2007 alrighty!... success!... thank you for all your help everyone.. your were great and helped alot! Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-247693 Share on other sites More sharing options...
witakr Posted May 8, 2007 Author Share Posted May 8, 2007 Ok, this is a points tracking and storing system... With the help of the kind folks here I have worked out the kinks and this code functions proccesspoints.php <?php $sitepath = "http://www.YOURSITE.com";// not currently used $scriptdir = "scorekeeper/";// you may change this to what ever the name of the folder is that contains the index.php file $filesdir = "playerfiles/"; // not used much but don't delete this anyway... $addpoints = $_POST["addpoints"];// gets var info with register_globals off $playername = $_POST["playername"];// gets var info with register_globals off $pnlower = strtolower($playername);// converts all letters to lowercase so it can be used fo the file name(helpful on some *NIX systems) $filename = $pnlower.".txt"; // sets the file name if( !$playername ) { echo "You need to enter a playername<br><a href='index.php'>[ Go Back ]</a>"; exit; } if( $playername == "-----SELECT PLAYER-----" ) { echo "You need to enter a playername<br><a href='index.php'>[ Go Back ]</a>"; exit; } if( $addpoints <= 0 or !$addpoints ) { echo "You need to enter a points value that is greater than zero.<br><a href='index.php'>[ Go Back ]</a>"; exit; } //$ps = fopen("playerfiles/$playerfile", "w+"); //if(!$ps){ echo "<BR><BR><B>Unable to write to file(".$playerfile."), Contact system administrator</b>"; } // else { $handle = fopen("playerfiles/$filename", "r"); @ $old_score = fread($handle, filesize("playerfiles/$filename")); fclose($handle); if ($old_score <= 0) { $old_score = "0"; } $add_score = $addpoints; $new_score = $old_score + $add_score; $handle = fopen("playerfiles/$filename", "w"); fwrite($handle, $new_score); fclose($handle); echo "<b>"; echo "Score updated for ".$playername."<br>"; echo "Old score: ".$old_score."<br>"; echo "Added this: ".$add_score."<br>"; echo "New Score: ".$new_score."<br>"; echo $old_score." + ".$add_score." = ".$new_score."<br>"; echo "<a href='index.php'>[ Go Back ]</a>"; echo "</b>"; ?> [\php] index.php [code=php:0] <?php $sitepath = "http://www.YOURSITE.com";// not currently used $scriptdir = "scorekeeper/";// you may change this to what ever the name of the folder is that contains the index.php file $filesdir = "playerfiles/"; // not used much but don't delete this anyway... ?> <br><BR><BR> <center> <table width="300" style="border: 1px double #000000"> <tr><td colspan=2 align=center> Scores </t></tr> <?php $files = glob("playerfiles/*.txt"); foreach ($files AS $file) { $filecon = file_get_contents($file); $name = basename($file, '.txt'); $cname = ucwords($name); echo "<tr><td width='75%' align='left' style='border: 1px dashed #000000'>".$cname."</td><td width='25' align='right' style='border: 1px dashed #000000'>".$filecon."</td></tr>"; } ?> </table> <form name='Score Keeper' action='<?php echo $sitepath.$scriptdir; ?>processpoints.php' method='Post' enctype='multipart/form-data' onsubmit='return verifyMe();'> <table class='table_form_1' id='table_form_1' cellspacing='0'> <tr> <td class='ftbl_row_1' ><LABEL for='playername'>Player Name: </td> <td class='ftbl_row_1a' > <SELECT name='playername' id='playername'> <OPTION>-----SELECT PLAYER----- <?php $files = glob("playerfiles/*.txt"); foreach ($files AS $file) { $name = basename($file, '.txt'); $cname = ucwords($name); echo "<OPTION>".$cname; } ?> </SELECT> </td> </tr> <tr> <td class='ftbl_row_2' ><LABEL for='addpoints'>Add Points: </td> <td class='ftbl_row_2a' ><input type='text' name='addpoints' id='addpoints' size='25' value=''> </td> </tr> <tr> <td colspan='2' align='right'><input type='submit' name='submit' value='Submit'> <input type='reset' name='reset' value='Reset'> </td> </tr> </table></center> If anyone wants to use it, feel free, you just need to have a folder within your scriptfolder named "playerfiles" that which contains the player files.. at this time you will need to create each file for each player and then chmod it but, hey, my 1st script outside form mail!...lol thanks for the help again everyone Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-247697 Share on other sites More sharing options...
igor berger Posted May 8, 2007 Share Posted May 8, 2007 Why are you supressing the errors with "@" @ $old_score = fread($handle, filesize("playerfiles/$filename")); Are you still getting an error when you use this? Maybe you need to check what happens if file does not exists. Does it create a new file for you? If not maybe you need to set the dir to 766 But this is a high securety risk.......need to test. Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-247826 Share on other sites More sharing options...
witakr Posted May 8, 2007 Author Share Posted May 8, 2007 yes.. i get a fread() error sayiing that the value must be greater than 0... this is only when the file doesnt contain anything... after a value greater than 0 is written then it works fine... i suppressed the error because its not a big deal and i was going to work on it later Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-248098 Share on other sites More sharing options...
igor berger Posted May 8, 2007 Share Posted May 8, 2007 There is a PHP function called something fileexists or something! You can use it to check if there is a file and also what size of the file, I think. IF it does not tell you the size of the file, I am sure you can find another function that will. You can set the topic solved, if you like. Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-248104 Share on other sites More sharing options...
witakr Posted May 8, 2007 Author Share Posted May 8, 2007 the function is file_exists().... i thought of that but the problem isnt if the file exists rather the fact that the new file doesnt contain a value when the fread() reads the file. Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-248205 Share on other sites More sharing options...
igor berger Posted May 8, 2007 Share Posted May 8, 2007 Great website www.php.net check it out for some function to check the size of the file.... Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-248212 Share on other sites More sharing options...
witakr Posted May 8, 2007 Author Share Posted May 8, 2007 ok, i changed it up a little... check it out... this allows for the file to be read without causing error then resets the var to 0 prior to performing the equation $handle = fopen("playerfiles/$filename", "w"); //opens to write a value to file - this avoids less than 0 error fwrite($handle, "1"); // write "1" to file fclose($handle); $handle = fopen("playerfiles/$filename", "r"); //opens same file for reading $old_score = fread($handle, filesize("playerfiles/$filename")); //read file and save to var fclose($handle); if ($old_score == 1) { $old_score = "0"; } //if the var is set to 1(new file) then set it to 0 then move on EDIT: I just looked at this code again and this code my cause another problem...,. one sec.... Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-248220 Share on other sites More sharing options...
igor berger Posted May 8, 2007 Share Posted May 8, 2007 Why rite a 1 you should write a 0 But check to see if file does not exist first before writing a zero to it. Link to comment https://forums.phpfreaks.com/topic/50314-solved-getting-file-contents/#findComment-248232 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.