Compare files with PHP

Asked

Viewed 1,324 times

0

Is there any tool or way to create a file comparison with PHP? Would you like something similar to Beyond Compare but within the Navigator.

We have an application change control system that we developed here, and this system is web, so we wanted to create something that could control who was and what source files were replaced by the programmers, but as they are used to Beyond Compare we would like to do something similar.

  • 1

    What have you tried to implement so far ? What is your specific question?

  • What is the problem of comparing the byte-to-byte files? Or the checksum/hash of both?

2 answers

3


I found this class here [mirror] that implements a Diff in PHP. I tested with a file .json simple, making a copy and deleting an object, the result is:


teste php diff

And this is PHP test file:

<html><head>
    <style>
    .diff td{
      vertical-align : top;
      white-space    : pre;
      white-space    : pre-wrap;
      font-family    : monospace;
    }
    .diffUnmodified { background-color: #BAF4FA; }
    .diffDeleted { background-color: #EEB4B4; }
    .diffInserted { background-color: #A9F2A4; }
    </style>
</head>
<body>
    // output the result of comparing two files as a table
    <?php 
    require_once './class.Diff.php';
    echo Diff::toTable( Diff::compareFiles('calendar-1.json', 'calendar-2.json') ); 
    ?>
</body></html>

1

There is a form in example #1 in the PHP manual xdiff_file_diff() which makes unified diff of two PHP files with context length of 2.

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

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

Remembering that it is not suitable for binary files.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.