1

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?

3 Answers 3

5

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"/>
Sign up to request clarification or add additional context in comments.

Comments

2

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

8 Comments

The output was "array(0) { } int(1)"
I think it's operation not permitted :) pegasoft.ca/resources/boblap/99_b.html
The destination directory does not exist, is not writeable or the 'diff' prog was not found / not executable.
@symcbean can't be "'diff' prog was not found / not executable." because he said it worked in console... @wilsonsilva have you tried creating the output file manually before executing the command? something like file_put_contents('/var/local/out/patch.patch','');chmod('/var/local/out/patch.patch',0777); ?
not really..it should have created the file :) for testing, try changing the permission on /var/local/out to 0777 from command line and try again :)
|
2

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

5 Comments

The output was "array(0) { }", I have no clue what it means. I'm still a Linux newbie.
@wilsonsilva From your other comment, you said the status returned was 1. This means there was an error. Do your paths point to files that exist?
Yes. If I execute the following command directly in linux: "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" the patch.patch file is created.
@wilsonsilva You want to read that file into PHP? Try using file_get_contents().
I'm unable to read it because the file isn't created if I use the php script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.