mikelmao Posted October 14, 2008 Share Posted October 14, 2008 Hello. I wana know how can i update an existing file? ok lets say there is a file called, michael.txt in michael.txt there is: username = michael password = hello_world age = 13 real_name = michael last_name = hello_world Now threw my website i want to change the password (hello_world) to hello-world. How can i do this. How can PHP Find the veriable password and update the value. please help me on this. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/128429-updating-a-file-threw-php/ Share on other sites More sharing options...
trq Posted October 14, 2008 Share Posted October 14, 2008 <?php exec("sed -i 's/hello_world/hello-world/' micheal.txt"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/128429-updating-a-file-threw-php/#findComment-665491 Share on other sites More sharing options...
mikelmao Posted October 14, 2008 Author Share Posted October 14, 2008 WTH?? Explain Quote Link to comment https://forums.phpfreaks.com/topic/128429-updating-a-file-threw-php/#findComment-665495 Share on other sites More sharing options...
wildteen88 Posted October 14, 2008 Share Posted October 14, 2008 Is the password always going to be on the second line? if so you could do $lines = file('michael.txt'); $lines = array_map('trim', $lines); // update the second line. $lines[1] = 'password = new_password'; // update the file. file_put_contents('michael.txt', implode(PHP_EOL, $lines)); Quote Link to comment https://forums.phpfreaks.com/topic/128429-updating-a-file-threw-php/#findComment-665496 Share on other sites More sharing options...
DarkWater Posted October 14, 2008 Share Posted October 14, 2008 <?php exec("sed -i 's/hello_world/hello-world/' micheal.txt"); ?> I'll admit that he wasn't being too specific, but I doubt you'd want to use sed to find the specific value and replace it like that if you don't know the value off hand. Quote Link to comment https://forums.phpfreaks.com/topic/128429-updating-a-file-threw-php/#findComment-665502 Share on other sites More sharing options...
mikelmao Posted October 14, 2008 Author Share Posted October 14, 2008 o and how do check wich files are in a Folder.. so like: users/[name of file].txt and echo the values just like doing so: <?php $result = mysql_query("SELECT * FROM users"); while($users = mysql_fetch_array($result)) { echo $users['name']; } ?> but then not in MYSQL.. in a folder. Quote Link to comment https://forums.phpfreaks.com/topic/128429-updating-a-file-threw-php/#findComment-665519 Share on other sites More sharing options...
wildteen88 Posted October 14, 2008 Share Posted October 14, 2008 You can use glob() for that foreach (glob("users/*.txt") as $userfile) { echo "$userfile<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/128429-updating-a-file-threw-php/#findComment-665521 Share on other sites More sharing options...
mikelmao Posted October 14, 2008 Author Share Posted October 14, 2008 one last.. this one is harder.. Lets say in that file thers something called membership: Values are: membership = 1 or membership = 0 1 = true 0 = false. how could i make it so that the value will be 1 but after 31 days the value changes to 0 Quote Link to comment https://forums.phpfreaks.com/topic/128429-updating-a-file-threw-php/#findComment-665601 Share on other sites More sharing options...
DarkWater Posted October 14, 2008 Share Posted October 14, 2008 Wait, do you have a file for each user? Use a database. EDIT:5000th post! Quote Link to comment https://forums.phpfreaks.com/topic/128429-updating-a-file-threw-php/#findComment-665604 Share on other sites More sharing options...
mikelmao Posted October 15, 2008 Author Share Posted October 15, 2008 i may not use a DB... it needs to be in a file.. And Gratz dude.. 5000TH Post on my Thread.. Im Honerd =) Quote Link to comment https://forums.phpfreaks.com/topic/128429-updating-a-file-threw-php/#findComment-665684 Share on other sites More sharing options...
mikelmao Posted October 16, 2008 Author Share Posted October 16, 2008 How can i check the value of the veriable tho? username = michael password = hello_world age = 13 real_name = michael last_name = hello_world Veriable = age Value of Veriable = 13 i dont care if its line 2.. just the value of the veriable i need to know. Quote Link to comment https://forums.phpfreaks.com/topic/128429-updating-a-file-threw-php/#findComment-666657 Share on other sites More sharing options...
trq Posted October 16, 2008 Share Posted October 16, 2008 Post the code your having problems with, were not here to write this for you. Quote Link to comment https://forums.phpfreaks.com/topic/128429-updating-a-file-threw-php/#findComment-666664 Share on other sites More sharing options...
Lamez Posted October 16, 2008 Share Posted October 16, 2008 May I ask, are you doing this for a RS server? Quote Link to comment https://forums.phpfreaks.com/topic/128429-updating-a-file-threw-php/#findComment-666678 Share on other sites More sharing options...
mikelmao Posted October 16, 2008 Author Share Posted October 16, 2008 yes (A) code: <?php if($_SERVER['REQUEST_METHOD'] == 'POST') { if(!empty($_POST['uname']) && !empty($_POST['pass'])) { $filename = "fsd64sdf654sdf65s4ddzzfsdfa654/characters/". $_POST['uname'] .".txt"; if(file_exists($filename)) echo "Already exists"; else { $h = fopen($filename, "w+"); $somecontent = "[ACCOUNT] character-username = ". $_POST['uname'] ." character-password = ". $_POST['pass'] ." [CHARACTER] character-height = 0 character-posx = 2721 character-posy = 4904 character-rights = 0 character-ismember = 0 character-messages = 0 character-lastconnection = 0 character-lastlogin = 0 character-energy = 100 character-gametime = 0 character-gamecount = 76 [EQUIPMENT] character-equip = 0 1153 0 character-equip = 1 -1 0 character-equip = 2 -1 0 character-equip = 3 1333 0 character-equip = 4 1115 0 character-equip = 5 1191 0 character-equip = 6 -1 0 character-equip = 7 2497 0 character-equip = 8 -1 0 character-equip = 9 -1 0 character-equip = 10 4121 0 character-equip = 11 -1 0 character-equip = 12 -1 0 character-equip = 13 -1 0 [LOOK] character-look = 0 0 character-look = 1 7 character-look = 2 8 character-look = 3 9 character-look = 4 5 character-look = 5 0 [sKILLS] character-skill = 0 1 0 character-skill = 1 1 0 character-skill = 2 1 0 character-skill = 3 10 1154 character-skill = 4 1 0 character-skill = 5 1 0 character-skill = 6 1 0 character-skill = 7 1 0 character-skill = 8 1 0 character-skill = 9 1 0 character-skill = 10 1 0 character-skill = 11 1 0 character-skill = 12 1 0 character-skill = 13 1 0 character-skill = 14 1 0 character-skill = 15 1 0 character-skill = 16 1 0 character-skill = 17 1 0 character-skill = 18 1 0 character-skill = 19 1 0 character-skill = 20 1 0 character-skill = 21 1 0 character-skill = 22 1 0 character-skill = 23 1 0 character-skill = 24 1 0 [iTEMS] character-item = 0 996 150000000 [bANK] [FRIENDS] [iGNORES] [EOF] "; if (!$handle = fopen($filename, 'a')) { echo "Some error has accured: Error: 21568"; exit; } if (fwrite($handle, $somecontent) === FALSE) { echo "Some error has accured: Error: 69871"; exit; } fclose($handle); echo "Account created, You have been givin 150M. You can now log in."; } } else { echo "You have left one or more fields blank."; } } else { ?> <form method='post'> Username: <input type='text' name='uname' maxlength='12'><br /> Password: <input type='password' name='pass' maxlength='18'><br /> <input type='submit' value='Register'> </form> <?php } ?> As you see.. Thats the register page.. Lets say i want to know the value of character-look = 0 25 Value of it is 25. How can i check it? plz help thx. Quote Link to comment https://forums.phpfreaks.com/topic/128429-updating-a-file-threw-php/#findComment-666983 Share on other sites More sharing options...
mikelmao Posted October 18, 2008 Author Share Posted October 18, 2008 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/128429-updating-a-file-threw-php/#findComment-668581 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.