在Linux中使用php的exec函数使用diff
我必须比较 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 中执行它,它运行得很好。但脚本本身不会生成补丁文件。可能出什么问题了?
I have to compare 2 xml files and generate a patch using php and Linux's diff command. Here's my code:
<?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);
?>
The browser outputs:
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
If I copy and paste the output and execute it directly in Linux, it runs just fine. But the script itself won't generate the patch file. What could be wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PHP 有一个针对 xdiff 的 PECL 扩展,您可能想尝试一下。
xdiff_file_diff
之间没有区别
请注意 diff 仅在语法层面上,而在语义上和
PHP has a PECL extension for xdiff you might want to try instead.
Example from Manual for
xdiff_file_diff
Note that diff is on the syntactical level only, while semantically there is no difference between
and
尝试检查执行命令的输出,也许您运行 apache 的用户没有权限在该文件夹或其他内容中写入...:
Try checking the output of the executed command, maybe the user you are running apache under doesn't have permission to write in that folder or somethin...:
您想像这样捕获输出...
您可以像这样重新组装线路...
You want to capture the output like so...
You can reassemble the lines like so...