Help! Writing in an exact point of a file.

Discussion in 'PHP' started by WindScar, Aug 28, 2006.

  1. #1
    Look the following file:

    <?xml version="1.0"?>
    <account pass="285682" type="1" premDays="0" lastsaveday="233"><characters><character name="Bombeta Branca"/></characters></account>

    I want to put a new character on it, like that:

    <?xml version="1.0"?>
    <account pass="285682" type="1" premDays="0" lastsaveday="233"><characters><character name="Bombeta Branca"/><character name="Bravo Azul"/></characters></account>

    But the code I have dont work, it delete all characters in the file and only the new one works.

    function createaccount(){
    include('config.php');
    $accdir = $datadir . '/accounts/';
    $playerdir = $datadir . '/players/';
    $voc = $_POST['voc'];
    $sex = $_POST['sex'];
    $name = $_POST['name'];
    $name = str_replace('"','',$name);
    $wpn = 2404;
    $account = @$_COOKIE['acc'];
    $password = @$_COOKIE['pass'];
    $xml = ".xml";
    $z = 0;
    $arquivo = @file_get_contents("$accdir/$account.xml");
    $procura = explode('"', $arquivo);
    $charname = array();
    $plik = file($accdir.$account.$xml);
    for($i = 0; $i<count($plik);$i++){
    $plik[$i] = explode('"',$plik[$i]);
    $plik[$i][0] = str_replace("\r\n", "", $plik[$i][0]);
    if($plik[$i][0] == '<character name='){
          $charname[$z] = $plik[$i][1];
          $z = $z + 1;
          }
    }
    
    $tresc =
    '<?xml version="1.0"?>
    <account pass="'.$password.'" type="1" premDays="'.$procura[7].'">
    <characters>';
    for($i = 0;$i<$z;$i++){
    $tresc= $tresc.'
    <character name="'.$charname[$i].'"/>
    ';}
    $tresc = $tresc.'
    <character name="'.$name.'"/> ';
    $tresc =$tresc.'
    </characters>
    </account>';
    unlink($accdir.$account.$xml);
    $plik = fopen($accdir.$account.$xml,"w");
    fwrite($plik,$tresc);
    fclose($plik);
    createplayer();
    }
    Code (markup):
     
    WindScar, Aug 28, 2006 IP
  2. jhunt

    jhunt Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What are you trying to do, exactly: Are you trying to edit one of the <account> records, or are you prepending a <account> record to the XML document?
     
    jhunt, Aug 28, 2006 IP