Jump to content

fwrite preg_replace


echoCarlos

Recommended Posts

Hi guys I'm tying to update the config.inc.php with the database info from the install.php

 

Install.php

 

$string = '<?php 

        $db_host = "'. $_POST["host"]. '";

        $db_user = "'. $_POST["username"]. '";

        $db_pass = "'. $_POST["password"]. '";

        ?>';



        $fp = fopen("config.inc.php", "a");

        fwrite($fp, $string);

        fclose($fp);

 

config.inc.php

$db_host = '';
$db_user = '';
$db_pass = '';

// connect to the server
$con = mysql_connect($db_host,$db_user,$db_pass) 
    or die('Failed to connect to the server');

// connect to the database
$db = mysql_select_db('marketplace')
    or die('Failed to connect to the database');

 

can someone help me on how id do this

Link to comment
https://forums.phpfreaks.com/topic/238842-fwrite-preg_replace/
Share on other sites

Well as your title suggests you've figured out that you need to use preg_replace. Try this:

 

$contents = file_get_contents('config.inc.php');

$regex = array(
'expressions' => array(
	'/\$db_host = \'\';/',
	'/\$db_user = \'\';/',
	'/\$db_pass = \'\';/'
),
'data' => array(
	'$db_host = \'' . $_POST['host'] . '\';',
	'$db_user = \'' . $_POST['username'] . '\';',
	'$db_pass = \'' . $_POST['password'] . '\';'
)
);

$contents = preg_replace($regex['expressions'], $regex['data'], $contents);

// fwrite $contents back to config.inc.php

Link to comment
https://forums.phpfreaks.com/topic/238842-fwrite-preg_replace/#findComment-1227272
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.