2

我正在处理一个应该替换另一个文件中的变量的文件。到目前为止,我尝试过:

$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 是用户输入。注释是数据库中需要替换为前缀_$dir 的表。

我做错了什么?

4

1 回答 1

2

您忘记再次写入文件。

// 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);

但是,正如 Zerkms 所说(或至少打算这样做 ;-)),使用 PHP 编辑 PHP 通常不是一个好主意,尤其是因为您稍后会发现很难调试脚本,因为它的代码会在运行时动态更改。

是否有任何理由不能包含此文件并手动设置这些变量,例如:

// Include the file    
require("$dir/submit.php"); 
// Edit the variable
$chosendb = "wuhuws_$dir";
于 2012-05-22T20:50:26.783 回答