Page 1 of 1

str_replace() & fwrite()

Posted: Sat Apr 12, 2003 11:12 pm
by Black Majic
I'm trying to get a php file to edit another php file, for some reason, it isnt working. Thoughts? Suggestions?

index.php (chmod'd to 777)

Code: Select all

<?php
 // DB_NAME - The name of the database
 define("DB_NAME", "%DB_NAME%");

 // DB_USER - The username to connect to the database as
 %DB_USER%
?>
edit.php

Code: Select all

<?php
	echo "<form action="$PHP_SELF" method="post"><input type="hidden" name="step" value="editfile">";
?>
<input name="file" value="./index.php">
<input name="dbname" value="foo">
<input name="dbuser" value="bar">
<!-- Other info -->
<?php
 $handle=fopen($file,"r+");
  $editphp=fread($handle, filesize($file));
// tried this
   $editphp=str_replace("%DB_NAME%", "$dbname", "define "DB_NAME", "%DB_NAME%");");
// and this
   $editphp=str_replace("%DB_USER%", "define("DB_USER", "$dbuser");", "");
  fwrite($handle, $editphp);
 fclose($handle);
?>

Posted: Sun Apr 13, 2003 6:05 am
by volka
please explain step by step what you're trying to do.

Posted: Sun Apr 13, 2003 1:50 pm
by Black Majic
I am attempting to get edit.php to write to index.php.
I would like %VARS_LIKE_THIS% in index.php to be replaced with the results of a form in edit.php.

1. User fills in form.
2. edit.php edits index.php
3. index.php is now usable.