Problem with fwrite() function

Discussion in 'PHP' started by Leosan, Sep 2, 2009.

  1. #1
    I am trying to make by blog software as user friendly as possible, to achieve this I am wanting to make a simple install.php

    All information which needs to be written to php files has been moved into one file called connect.php

    My problem is that the fwrite is not functioning correctly.

    This is the current install file, just the write portion
    <?php
    // Variables which will be changed by Installer Input
    $dbuser='Cole';
    $dbpassword='teflon';
    $dbmaster = 'main';
    $dbslave = 'slave';
    $dbsiteid = '1';
    $dbprefix = 'blog';
    
    
    // Config.php Writer
    $file = "connect.php";
    $fh = fopen("connect.php",'r+');
    if($fh){
      $data = file_get_contents($file);
      rewind($fh);
      $data = str_replace("dbuser","$dbuser",$data);
      $data = str_replace("dbpass","$dbpassword",$data);
      $data = str_replace("dbnameone","$dbmaster",$data);
      $data = str_replace("dbnametwo","$dbslave",$data);
      $data = str_replace("dbsiteid","$dbsiteid",$data);
      $data = str_replace("dbprefix","$dbprefix",$data);
      fwrite($fh,$data);
    fclose($fh);
    }
    echo "Writing Completed";
    ?>
    Code (markup):
    Here is the connect.php which its to write to
    <?php
    mysql_connect("localhost", "dbuser", "dbpass"); 
    mysql_select_db("dbnameone");
    mysql_select_db("dbnametwo");
    $siteid = "dbsiteid";
    $prefix = "dbprefix";
    $dbmast = "dbnameone";
    $dbslave = "dbnametwo"; 
    ?>
    Code (markup):
    The result of running the index.php should be
    <?php
    mysql_connect("localhost", "Cole", "teflon"); 
    mysql_select_db("main");
    mysql_select_db("slave");
    $siteid = "1";
    $prefix = "blog";
    $dbmast = "main";
    $dbslave = "slave"; 
    ?>
    Code (markup):
    But instead, its writing the file as
    <?php
    mysql_connect("localhost", "Cole", "teflon"); 
    mysql_select_db("main");
    mysql_select_db("slave");
    $siteid = "1";
    $prefix = "blog";
    $dbmast = "main";
    $dbslave = "slave"; 
    ?>;
    $dbslave = "dbnametwo"; 
    ?>
    Code (markup):
    Can anyone help with this, its really boggling my brain.
     
    Leosan, Sep 2, 2009 IP