...

View Full Version : Resolved fwrite issue: cuts of existing data when r+ (start top of file)



fl00d
03-02-2009, 06:05 AM
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 ge = $gen_message."\n".mysql_error();. It should start with $connect = mysql_connect.... 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
-fl00d

fl00d
03-02-2009, 07:10 AM
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.

ninnypants
03-02-2009, 07:18 AM
Try using file_get_contents and replacing certain strings with a string replace

Original config

<?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

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


$file = 'generated content';
$file .= file_get_contents('config.php');

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

oesxyl
03-03-2009, 03:36 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum