SC__Programmer Posted August 14, 2011 Share Posted August 14, 2011 I need some help on opening files,then reading it and saving it to a variable then closing it. Then i want to re-open the file,add 1 to its value of the file i opened,then close it. Ive tryed to look at the manual,but i cant figure it out =\ Link to comment https://forums.phpfreaks.com/topic/244761-file-help/ Share on other sites More sharing options...
voip03 Posted August 14, 2011 Share Posted August 14, 2011 $ourFileName = "testFile.txt"; $fh = fopen($ourFileName, 'X') or die("Can't open file"); fclose($fh); http://php.net/manual/en/function.fopen.php http://www.w3schools.com/PHP/php_file.asp Link to comment https://forums.phpfreaks.com/topic/244761-file-help/#findComment-1257165 Share on other sites More sharing options...
AbraCadaver Posted August 14, 2011 Share Posted August 14, 2011 I am assuming that the file contains a number? $num = file_get_contents('/path/to/file.txt'); file_put_contents($num + 1, '/path/to/file.txt'); Link to comment https://forums.phpfreaks.com/topic/244761-file-help/#findComment-1257180 Share on other sites More sharing options...
SC__Programmer Posted August 14, 2011 Author Share Posted August 14, 2011 it does,im trying to make a point system for my forum members,and protecting other people from stealing their points with "$_SERVER['HTTP_REFERER'];". the code you gave me wont work,maybe you will be able to figure out whats wrong with it from my full code =\ heres my full code, <body bgcolor="#ababab"> <meta http-equiv="REFRESH" content="3;url=http://mysite.co.cc/points"> <?php $idnumber = $_GET["id"]; $idfile = "http://mysite.co.cc/points/id/".$idnumber.".txt"; $points = file_get_contents($idfile); $reftrue = ""; $ref = $_SERVER['HTTP_REFERER']; if ( $ref == "url" || $ref == "other url" ) { $reftrue = "yes"; }else{ $reftrue = "no"; } if ( $reftrue == "yes" ) { ?> <title>Congrats!</title> <?php echo "You Have Been Awarded 1 Point."; file_put_contents($points + 1, $idfile); } if ( $reftrue == "no" ) { echo "Trying To Cheat,Eh?Well Dont Think About It!"; ?> <title>Cheat Alert!</title> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/244761-file-help/#findComment-1257308 Share on other sites More sharing options...
xyph Posted August 14, 2011 Share Posted August 14, 2011 You want to use the local path to the file, not the web URI Link to comment https://forums.phpfreaks.com/topic/244761-file-help/#findComment-1257317 Share on other sites More sharing options...
SC__Programmer Posted August 14, 2011 Author Share Posted August 14, 2011 ok well i changed it to this, $idfile = "/home1/sc/public_html/points/gp/id/".$idnumber.".txt"; but its creating a file called 1.txt in the gp folder =\ Link to comment https://forums.phpfreaks.com/topic/244761-file-help/#findComment-1257322 Share on other sites More sharing options...
xyph Posted August 14, 2011 Share Posted August 14, 2011 That's odd. Perhaps echo'ing $idfile will help you debug the issue. Does the /id/ folder exist? Link to comment https://forums.phpfreaks.com/topic/244761-file-help/#findComment-1257330 Share on other sites More sharing options...
SC__Programmer Posted August 14, 2011 Author Share Posted August 14, 2011 it exists,i cant figure out why its doing this =\ heres what it prints out in the 1.txt file, /home1/sc/public_html/points/gp/id/1.txt Link to comment https://forums.phpfreaks.com/topic/244761-file-help/#findComment-1257341 Share on other sites More sharing options...
xyph Posted August 14, 2011 Share Posted August 14, 2011 Perhaps you should read how to use the function http://www.php.net/manual/en/function.file-put-contents.php Link to comment https://forums.phpfreaks.com/topic/244761-file-help/#findComment-1257373 Share on other sites More sharing options...
SC__Programmer Posted August 14, 2011 Author Share Posted August 14, 2011 ok i figured it out. the file comes first lol Link to comment https://forums.phpfreaks.com/topic/244761-file-help/#findComment-1257432 Share on other sites More sharing options...
AbraCadaver Posted August 14, 2011 Share Posted August 14, 2011 Perhaps you should read how to use the function http://www.php.net/manual/en/function.file-put-contents.php Doh! My bad. And I use that function all the time Link to comment https://forums.phpfreaks.com/topic/244761-file-help/#findComment-1257453 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.