str_replace() & fwrite()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Black Majic
Forum Newbie
Posts: 6
Joined: Sat Apr 12, 2003 11:12 pm
Location: Etobicoke, ON, CA
Contact:

str_replace() & fwrite()

Post 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);
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

please explain step by step what you're trying to do.
Black Majic
Forum Newbie
Posts: 6
Joined: Sat Apr 12, 2003 11:12 pm
Location: Etobicoke, ON, CA
Contact:

Post 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.
Post Reply