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 はユーザー入力です。コメントは、prefix_$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 に答える