Jump to content

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


CMC

Recommended Posts

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
$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 isn't important as it shows up fine
?>

 

Initial config (before data is written)

$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.

$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 [icode]ge = $gen_message."\n".mysql_error();[/icode]. It should start with [icode]$connect = mysql_connect...[/icode]. Not sure what's going on.

 

install.php with fwrite

$config_data = "<?php\n\$db_host = '{$clean['host']}';\n\$db_user = '{$clean['user']}';\n\$db_passwrd = '{$clean['passwd']}';\n\$db_name = '{$clean['name']}';\n\n\$vb_prefix = '{$clean['vbpre']}';\n\$vb_dir = '{$clean['vbdir']}';\n\$ga_dir = '{$clean['gadir']}';\n\n\$html_title = '{$clean['title']}';\n\$site_name = '{$clean['sname']}';\n\n\n";

$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

-CMC

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.