Reputation: 111
I am working on a file that should replace a variable in another file. So far I tried:
$File = "$dir/submit.php";
$fh = fopen($File, 'r') or die("Couldn't edit the Config-file. Please report to admin.");
$chosendb = str_replace('$chosendb = comments;','$chosendb = wuhuws_$dir;','$chosendb');
fclose($fh);
$dir is a user input. comments is a table in the database that need to replaced with the prefix_$dir.
What do I do wrong?
Upvotes: 2
Views: 838
Reputation: 6882
You're forgetting to write to the file again.
// Read the file
$content = file_get_contents("$dir/submit.php");
// Do the editing
$content = str_replace('$chosendb = comments;','$chosendb = wuhuws_$dir;', $content);
// Save the file
file_put_contents("$dir/submit.php", $content);
However, as Zerkms said (or atleast intended to ;-) ), it's generally a bad idea to edit PHP with PHP, especially since you'll find a hard time debugging your script later since its code dynamically changes upon runtime.
Is there any reason why you cannot include this file and manually set these variables, like:
// Include the file
require("$dir/submit.php");
// Edit the variable
$chosendb = "wuhuws_$dir";
Upvotes: 2