0

我想在 xml 文件中替换服务器的地址。在线放置占位符%scr_path%

<property id="urlGenerateImage">%scr_path%/imgcap.php</property> 

使用以下代码

$path=$wsurl."core/contents/tests";
//read the entire string
$str=implode("\n",file('../includes/ckeditor/plugins/fmath_formula/dialogs/configMathMLEditor.xml'));

$fp=fopen('../includes/ckeditor/plugins/fmath_formula/dialogs/configMathMLEditor.xml','w');
//replace something in the file string 
$str=str_replace('%scr_path%',$path,$str);

//now, TOTALLY rewrite the file
fwrite($fp,$str,strlen($str));

得到一堆关于错误文件路径的错误。检查了两次路径。怎么了?

4

1 回答 1

1

这完全取决于您的脚本在哪个目录中运行。由于您使用的是相对路径名 (../),因此您最好确保知道脚本当前所在的目录。或者,更好的是,将其设置为脚本的开头,因此它将始终有效。

$path = $wsurl."core/contents/tests";

// change directory to project root
chdir("/your/project/directory");

// read the file into a string
$filename = 'includes/ckeditor/plugins/fmath_formula/dialogs/configMathMLEditor.xml';
$str = file_get_contents($filename);

// replace token
$str = str_replace('%scr_path%',$path,$str);

// save file
file_put_contents($filename, $str);
于 2011-11-23T00:54:16.427 回答