我必须比较2个xml文件并使用php和Linux的diff命令生成补丁。这是我的代码:
<?php
// script file location: /var/local/out/upload.php
// ...
// $templateName file location: /var/local/out/upload/example_word_template/word/document.xml
// $filename file location: /var/local/out/upload/example_word/word/document.xml
// $templateName value: upload/example_word_template/word/document.xml
// $filename value: upload/example_word/word/document.xml
$command = "diff /var/local/out/$templateName /var/local/out/$filename > /var/local/out/patch.patch";
exec($command);
echo($command);
?>
浏览器输出:
diff /var/local/out/upload/example_word_template/word/document.xml /var/local/out/upload/example_word/word/document.xml> /var/local/out/patch.patch
如果我复制并粘贴输出并直接在Linux中执行它,它运行得很好。但脚本本身不会生成补丁文件。可能有什么不对?
答案 0 :(得分:5)
PHP有一个PECL extension for xdiff你可能想尝试。
手册中的示例$old_version = 'my_script.php';
$new_version = 'my_new_script.php';
xdiff_file_diff($old_version, $new_version, 'my_script.diff', 2);
请注意,diff仅在语法层面上,而在语义上
之间没有区别<element foo="foo" bar="bar"/>
和
<element
foo="foo"
bar="bar"/>
答案 1 :(得分:2)
尝试检查已执行命令的输出,也许你正在运行apache的用户没有权限写入该文件夹或某些东西......:
$output=array();$status=0;
exec($command,$output,$status);
var_dump($output);var_dump($status);
答案 2 :(得分:2)
你想像这样捕获输出......
$command = "diff /var/local/out/$templateName /var/local/out/$filename > /var/local/out/patch.patch";
exec($command, $output);
var_dump($output);
你可以像这样重新组合线......
echo join("\n", $output);