在Linux中使用php的exec函数使用diff

发布于 2024-10-08 04:52:31 字数 846 浏览 0 评论 0原文

我必须比较 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一瞬间的火花 2024-10-15 04:52:31

PHP 有一个针对 xdiff 的 PECL 扩展,您可能想尝试一下。

xdiff_file_diff

$old_version = 'my_script.php';
$new_version = 'my_new_script.php';

xdiff_file_diff($old_version, $new_version, 'my_script.diff', 2);

之间没有区别

<element foo="foo" bar="bar"/>

请注意 diff 仅在语法层面上,而在语义上和

<element 
    foo="foo"
    bar="bar"/>

PHP has a PECL extension for xdiff you might want to try instead.

Example from Manual for xdiff_file_diff

$old_version = 'my_script.php';
$new_version = 'my_new_script.php';

xdiff_file_diff($old_version, $new_version, 'my_script.diff', 2);

Note that diff is on the syntactical level only, while semantically there is no difference between

<element foo="foo" bar="bar"/>

and

<element 
    foo="foo"
    bar="bar"/>
城歌 2024-10-15 04:52:31

尝试检查执行命令的输出,也许您运行 apache 的用户没有权限在该文件夹或其他内容中写入...:

$output=array();$status=0;
exec($command,$output,$status);
var_dump($output);var_dump($status);

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...:

$output=array();$status=0;
exec($command,$output,$status);
var_dump($output);var_dump($status);
梦里梦着梦中梦 2024-10-15 04:52:31

您想像这样捕获输出...

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

You want to capture the output like so...

$command = "diff /var/local/out/$templateName /var/local/out/$filename > /var/local/out/patch.patch";
exec($command, $output);

var_dump($output);

You can reassemble the lines like so...

echo join("\n", $output);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文