Php implementation to compare the similarities and differences between the two folders

Source: Internet
Author: User

Php implementation to compare the similarities and differences between the two folders

This example describes how to compare the similarities and differences between two folders in php. Share it with you for your reference. The specific analysis is as follows:

Requirements:

You can only use command lines to compare the differences between two folders, including the differences in files.

Thoughts:

In linux, diff .... Use php. The code is easy to modify and the speed is fast. The comparison of. svn directories is excluded below.

File to compare the md5 checksum

Ideas:

1) use the first path as the standard path to list files or folders not found in the first path, or different files.

2) then, list all the files and folders that do not exist in the 2nd paths.

Call example:

Php compare_folder.php/home/temp/2/home/temp/55

The Code is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

<? Php

/**

* Tool files

* The purpose is to recursively compare two folders

*

* Call example

* Php compare_folder.php/home/temp/2/home/temp/55

*

*/

// Confirm the Parameter

If (count ($ argv)> 1)

$ Dir1 = del_postfix ($ argv [1]);

Else

$ Dir1 = '/';

If (count ($ argv)> 2)

$ Dir2 = del_postfix ($ argv [2]);

Else

$ Dir2 = '/';

// Check whether the first path has a method that does not exist or is incorrect.

Process_compare ($ dir1, $ dir2, 0 );

Echo "============================================== =====================================\ n ";

// Check the excess folders or files in the 2nd paths

Process_compare ($ dir2, $ dir1, 1 );

Echo "all OK \ n ";

/**

* Remove the/at the end of the path and make sure it is an absolute path.

*

* @ Param unknown_type $ dir

* @ Return unknown

*/

Function del_postfix ($ dir)

{

If (! Preg_match ('# ^/#', $ dir )){

Throw new Exception ('the parameter must be an absolute path ');

}

$ Dir = preg_replace ('#/$ #', '', $ dir );

Return $ dir;

}

/**

* A common function calls a recursive method for comparison.

*

* @ Param string $ dir1 as the standard path

* @ Param string $ path used for comparison of dir2

* @ Param int $ only_check_has: 1 indicates no comparison of file differences. 0 indicates comparison of the md5 checksum of the file.

*/

Function process_compare ($ dir1, $ dir2, $ only_check_has ){

Compare_file_folder ($ dir1, $ dir1, $ dir2, $ only_check_has );

}

/**

* Real functions, private functions

*

* @ Param string $ dir1 Path 1, standard

* @ Param string $ base_dir1 constant parameter Path 2

* @ Param string $ base_dir2: Unchanged path to be compared 2

* @ Param int $ only_check_has: 1 indicates no comparison of file differences. 0 indicates comparison of the md5 checksum of the file.

*

*/

Function compare_file_folder ($ dir1, $ base_dir1, $ base_dir2, $ only_check_has = 0 ){

If (is_dir ($ dir1 )){

$ Handle = dir ($ dir1 );

If ($ dh = opendir ($ dir1 )){

While ($ entry = $ handle-> read ()){

If ($ entry! = ".") & ($ Entry! = "...") & ($ Entry! = ". Svn ")){

$ New = $ dir1. "/". $ entry;

// Echo 'compare: '. $ new. "\ n ";

$ Other = preg_replace ('# ^'. $ base_dir1. '#', $ base_dir2, $ new );

If (is_dir ($ new )){

// Compare

If (! Is_dir ($ other )){

Echo '!! Not found direction: '. $ other.' ('. $ new. ") \ n ";

}

Compare_file_folder ($ new, $ base_dir1, $ base_dir2, $ only_check_has );

} Else {// if 1 is a file, 2 should also be a file

If (! Is_file ($ other )){

Echo '!! Not found file: '. $ other.' ('. $ new. ") \ n ";

} Elseif ($ only_check_has = 0 & (md5_file ($ other )! = Md5_file ($ new ))){

Echo '!! File md5 error: '. $ other.' ('. $ new. ") \ n ";

}

}

}

}

Closedir ($ dh );

}

}

}

?>

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.