Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Mar 2007
    Location
    Quebec
    Posts
    261
    Thanks
    6
    Thanked 7 Times in 7 Posts

    fwrite issue: cuts of existing data when r+ (start top of file)

    Hi,

    Right so tonight I was working on my installer script, and I've run into a problem. Part of my installer script takes data inputted through a form and writes it to a config file. Since the config file already has data in it, I set fwrite to r+.

    The problem is, the written data from the install form overwrites some of the existing data in the config file.

    Ideal
    Here is what the finished copy of the config file should look like (after being written too)
    PHP Code:
    <?php
    $db_host 
    'localhost';
    $db_user 'data';
    $db_passwrd 'data';
    $db_name 'data';

    $vb_prefix 'vb';
    $vb_dir '/data/vb/';
    $ga_dir '/data/ga/';

    $html_title 'Gangsta Giveaways';
    $site_name 'MAXGames';

    $connect mysql_connect($db_host,$db_user,$db_passwrd); 
    if(!
    $connect){
        
    $gen_message 'An error was encountered while accessing the database. The connection to the database failed. Please try again later.';
        
    $email_message $gen_message."\n".mysql_error();
        
    #ReportAdmin($admin_email,$email_message);
        
    die($gen_message);
    }

    blah blah blah... rest isnt important as it shows up fine
    ?>
    Initial config (before data is written)
    PHP Code:
    $connect mysql_connect($db_host,$db_user,$db_passwrd); 
    if(!
    $connect){
        
    $gen_message 'An error was encountered while accessing the database. The connection to the database failed. Please try again later.';
        
    $email_message $gen_message."\n".mysql_error();
        
    #ReportAdmin($admin_email,$email_message);
        
    die($gen_message);
    }
    ?> 

    Reality:

    Final config: Here is the problem. the fwrite cuts of part of the data already in the config file.
    PHP Code:
    $db_host 'localhost';
    $db_user '';
    $db_passwrd '';
    $db_name '';

    $vb_prefix 'vb';
    $vb_dir '6';
    $ga_dir '/ho';

    $html_title 'Gangsta Giveaways';
    $site_name 'MAXGames';


    ge $gen_message."\n".mysql_error();
        
    #ReportAdmin($admin_email,$email_message);
        
    die($gen_message);

    See the ge = $gen_message."\n".mysql_error();. It should start with $connect = mysql_connect.... Not sure what's going on.

    install.php with fwrite
    PHP Code:
    $config_data = "<?phpn$db_host '{$clean['host']}';n$db_user '{$clean['user']}';n$db_passwrd '{$clean['passwd']}';n$db_name '{$clean['name']}';nn$vb_prefix '{$clean['vbpre']}';n$vb_dir '{$clean['vbdir']}';n$ga_dir '{$clean['gadir']}';nn$html_title '{$clean['title']}';n$site_name '{$clean['sname']}';nnn";
                
    $fwrite = fwrite($fh,$config_data,strlen($config_data)) or die('Config file was opened but not be written to.');
    I've used this method before with installers and I'vee never encountered this problem. Anyone have any ideas?

    Thanks
    -fl00d
    Last edited by fl00d; 03-02-2009 at 07:10 AM.

  • #2
    Regular Coder
    Join Date
    Mar 2007
    Location
    Quebec
    Posts
    261
    Thanks
    6
    Thanked 7 Times in 7 Posts
    I've found a temporary resolution. I added a ton of whitespace (new lines) to the original config file (250 blank lines before any text starts).

    Seems to work.

  • #3
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    Try using file_get_contents and replacing certain strings with a string replace

    Original config
    PHP Code:
    <?php
    $db_host 
    'host_loc';
    $db_user 'usr';
    $db_passwrd 'pwd';
    $db_name 'db_nm';

    $vb_prefix 'pf';
    $vb_dir 'v_dir';
    $ga_dir 'g_dir';

    $html_title 'ttl';
    $site_name 's_nm';
    ?>
    Editing the config
    PHP Code:
    <?
    $file 
    file_get_contents('config.php');

    str_replace('host_loc'$clean['host'], $file);
    str_replace('usr'$clean['user'], $file);
    str_replace('pwd'$clean['passwd'], $file);
    str_replace('db_nm'$clean['name'], $file);

    str_replace('pf'$clean['vbpre'], $file);
    str_replace('v_dir'$clean['vbdir'], $file);
    str_replace('g_dir'$clean['gadir'], $file);

    str_replace('ttl'$clean['title'], $file);
    str_replace('s_nm'$clean['sname'], $file);

    $f fopen('config.php','w');
    fwrite($f,$file);
    fclose($f);

    ?>
    or even something similar like appending the config files content to the generated content
    PHP Code:
    $file 'generated content';
    $file .= file_get_contents('config.php');

    $fp fopen('config.php');
    fwrite($fp$file);
    fclose($fp); 

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by fl00d View Post
    I've found a temporary resolution. I added a ton of whitespace (new lines) to the original config file (250 blank lines before any text starts).

    Seems to work.
    use a+ instead of r+ if you want to append data. see:

    http://www.php.net/manual/en/function.fopen.php

    best regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •