Jump to content

Updating a file threw PHP


mikelmao

Recommended Posts

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

Link to comment
Share on other sites

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));

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.