echoCarlos Posted June 8, 2011 Share Posted June 8, 2011 Hi guys, I have a php file called install.php and the user submits there host details, the thing is i also want to write those details to these define functions in includes/config.php with the content define('DB_HOST', 'change1'); define('DB_USER', 'change2'); define('DB_PASS', 'change3'); and overwrite change1 with host and user and pass how could this be done thanks Quote Link to comment https://forums.phpfreaks.com/topic/238830-write-variables-string-to-another-file/ Share on other sites More sharing options...
spiderwell Posted June 8, 2011 Share Posted June 8, 2011 you could use file write functions to achieve this, and rewrite the config.php file when the user submits the form. Quote Link to comment https://forums.phpfreaks.com/topic/238830-write-variables-string-to-another-file/#findComment-1227169 Share on other sites More sharing options...
echoCarlos Posted June 8, 2011 Author Share Posted June 8, 2011 thanks for the reply mate i found this would it work http://www.tizag.com/phpT/filewrite.php also how would i use this function to target chaneg1 for example thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/238830-write-variables-string-to-another-file/#findComment-1227176 Share on other sites More sharing options...
spiderwell Posted June 8, 2011 Share Posted June 8, 2011 if you have the 'blank' file saved already, use the file object to read the file into a variable, and use str_replace() to change it. $filetext = str_replace('change1','localhost',$filetext); assume i have read the file contents of the example you gave, into $filetext, I then ask it to look for 'change1' in that string and replace that with 'localhost', and pass it back to the $filetext. i would then write that variable to the file and save file . Quote Link to comment https://forums.phpfreaks.com/topic/238830-write-variables-string-to-another-file/#findComment-1227184 Share on other sites More sharing options...
echoCarlos Posted June 9, 2011 Author Share Posted June 9, 2011 Thanks for the help but i managed to find something which did the trick on Google will post here incase anyone needs it. <?php $string = '<?php $dbhost = "'. $_POST["dbhost"]. '"; $dbuname = "'. $_POST["dbuname"]. '"; $dbpass = "'. $_POST["dbpass"]. '"; $dbname = "'. $_POST["dbname"]. '"; $prefix = "'. $_POST["prefix"]. '"; $user_prefix = "'. $_POST["user_prefix"]. '"; $dbtype = "'. $_POST["dbtype"]. '"; ?>'; $fp = fopen("config.php", "w"); fwrite($fp, $string); fclose($fp); ?> Quote Link to comment https://forums.phpfreaks.com/topic/238830-write-variables-string-to-another-file/#findComment-1227228 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.