Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. <?php
  2.  
  3. try {
  4.  
  5. function _trim($str) {
  6. return rtrim(trim($str, ' \r\t\n\0\x0B'), ' /');
  7. }
  8.  
  9. # paths to compare
  10. $dir1 = isset($argv[1]) ? _trim($argv[1]) : null;
  11. $dir2 = isset($argv[2]) ? _trim($argv[2]) : null;
  12.  
  13. touch("/tmp/ORIGINAL.txt");
  14. touch("/tmp/DESTINATION.txt");
  15.  
  16. $tmp1 = "/tmp/ORIGINAL.txt";
  17. $tmp2 = "/tmp/DESTINATION.txt";
  18.  
  19. # file type
  20. $ftype = isset($argv[3]) ? '*.' . _trim($argv[3]) : null;
  21.  
  22. # file comparison
  23. $only_compare = false;
  24.  
  25. if('-files' === $dir1) {
  26. $only_compare = true;
  27. } elseif(null === $dir1 || null === $dir2 || null === $ftype) {
  28. throw new Exception("Provide all arguments", 1);
  29. }
  30.  
  31. if(false === $only_compare) {
  32.  
  33. # md5sum files
  34. print 'Hashing the files on '. $dir1 . '/' . $ftype . PHP_EOL;
  35. `md5sum "{$dir1}/"{$ftype} > {$tmp1}` . PHP_EOL;
  36.  
  37. print 'Hashing the files on '. $dir2 . '/' . $ftype . PHP_EOL;
  38. `md5sum "{$dir2}/"{$ftype} > {$tmp2}` . PHP_EOL;
  39. }
  40.  
  41. # compare
  42. print 'Comparing the files' . PHP_EOL;
  43. print `diff -q {$tmp1} {$tmp2}` . PHP_EOL;
  44.  
  45. # continue
  46. print 'Do you want to see the differences?' . PHP_EOL;
  47. $handle = fopen('php://stdin', 'r');
  48. $line = fgets($handle);
  49.  
  50. if('yes' == trim($line) || 'y' == trim($line)) {
  51. print `diff -y {$tmp1} {$tmp2}`;
  52. } else {
  53. throw new Exception("Exiting...", 2);
  54. }
  55.  
  56. } catch (Exception $e) {
  57.  
  58. print 'Code: ' . $e->getCode() . PHP_EOL;
  59. print 'Message: ' . $e->getMessage() . PHP_EOL;
  60. print 'To make the lists and compare do: ' . PHP_EOL;
  61. print 'php ./compare.php PATH_1 PATH_2 FILE_TYPE' . PHP_EOL;
  62. print 'To just compare lists do: ' . PHP_EOL;
  63. print 'php ./compare.php -files';
  64.  
  65. } finally {
  66. if(isset($handle)) {
  67. fclose($handle);
  68. }
  69.  
  70. print PHP_EOL;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement