Home > php教程 > PHP源码 > 上次分享了比较文本的差异,这次来个比较文件夹的改变

上次分享了比较文本的差异,这次来个比较文件夹的改变

PHP中文网
Release: 2016-05-25 17:01:06
Original
940 people have browsed it

这个类可以比较两个目录中的文件,并找到改变。
它遍历两个目录检索的文件列表。
这个类可以列出哪些文件被添加,删除或更改。

<?php
/*
* http://www.php.cn
*/
class compare
{

private $source;
private $update;
private $removed = array();
private $added = array();
private $changed = array();

public function __construct(){
}

public function set_source($source) {
$this->source = $source;
}

public function set_update($update) {
$this->update = $update;
}

public function get_source() {
return $this->source;
}

public function get_update() {
return $this->update;
}

public function get_removed() {
return $this->removed;
}

public function get_added() {
return $this->added;
}

public function get_changed() {
return $this->changed;
}

public function do_compare(){
 $source = Array();
 $destination = Array();
 $currentDirectory = getcwd();
 $workingDir = &#39;&#39;;
chdir($this->source);
 $source = $this->doTree(&#39;.&#39;, $source);
 if (!is_array($source)) return;
 chdir ($this->update);
 $destination = $this->doTree(&#39;.&#39;, $destination);
 if (!is_array($destination)) exit;
chdir($currentDirectory);
 foreach ($source as $dir => $value) {
 foreach($value as $file => $hash) {
 if (isset($destination[$dir][$file])) {
 if ($hash != $destination[$dir][$file]) {
 $this->changed[] = $dir.&#39;/&#39;.$file;
}
 } else {
 $this->removed[] = $dir.&#39;/&#39;.$file;
}
}
}
 foreach ($destination as $dir => $value) {
 foreach($value as $file => $hash) {
 if (!isset($source[$dir][$file])) {
 $this->added[] = $dir.&#39;/&#39;.$file;
}
}
}
}

private function checksum($file) {
 $ignores = Array(10, 13);
 $fh = fopen($file, &#39;r&#39;);
 $buffer = &#39;&#39;;
 while (!feof($fh)) {
 $buffer .= fgets($fh);
}
 fclose ($fh);
 foreach ($ignores as $ignore) {
 while (strpos($buffer, chr($ignore))) {
 $buffer = str_replace(chr($ignore), &#39;&#39;, $buffer);
}
}
 return hash(&#39;crc32&#39;, $buffer).hash(&#39;crc32b&#39;, $buffer);
}

private function doTree($dir, &$array) {
 if (stripos($dir, &#39;checksum&#39;) !== false
 || stripos($dir, &#39;cache&#39;) !== false
 || stripos($dir, &#39;import&#39;) !== false
 || stripos($dir, &#39;custom&#39;) !== false
 || stripos($dir, &#39;_notes&#39;) !== false
 || stripos($dir, &#39;.svn&#39;) !== false) {
 return $array;
}
 $filetypes = Array(&#39;php&#39;, &#39;js&#39;, &#39;htm&#39;, &#39;html&#39;, &#39;css&#39;, &#39;tpl&#39;, &#39;ini&#39;, &#39;txt&#39;);
 if ($dh = opendir($dir)) {
 while ($file = readdir($dh)) {
 if ($file != &#39;.&#39; && $file != &#39;..&#39;) {
 if (is_dir($dir.&#39;/&#39;.$file)) {
 if (count($array) == 0) $array[0] = &#39;Temp&#39;;
 if (!$this->doTree($dir.&#39;/&#39;.$file, $array)) {
 return false;
}
 } else {
 if (filesize($dir.&#39;/&#39;.$file)) {
 foreach ($filetypes as $type) {
 if (strpos($file.&#39;|&#39;, &#39;.&#39;.$type.&#39;|&#39;) != 0 ) {
set_time_limit(30);
 $array[$dir][$file] = $this->checksum( $dir.&#39;/&#39;.$file ); //md5_file($dir.&#39;/&#39;.$file, false);
}
}
}
}
}
}
 if (count($array) > 1 && isset($array[&#39;0&#39;])) unset($array[&#39;0&#39;]);
 return $array;
 } else {
 echo &#39;error opening &#39;.$dir.&#39;</h3>&#39;;
 return false;
}
}

}
Copy after login
<?php
/*
* http://www.php.cn
*/
?>
<html>

<head>
<title>http://www.codepearl.com</title>
</head>
<body>
<h1 align="center">Upgrade checker</h1>
<?php
 include (&#39;compare.class.php&#39;);// Load the class
 $cmp = new compare();// Initialize the class
 $dir = dirname(__FILE__);
 $cmp->set_source($dir.&#39;Source&#39;);// Directory where Source files are
 $cmp->set_update($dir.&#39;Update&#39;);// Directory where pristeen files are
 $cmp->do_compare();// Do the compare
 $removed = $cmp->get_removed();// Get the results
 $added = $cmp->get_added();// ...
 $changed = $cmp->get_changed();// ...
 echo &#39;<h3>Files Changed</h3>&#39;;// Display the results
 foreach($changed as $file) {
 echo $file.&#39; has been changed<br>&#39;;
}
 echo &#39;<h3>Files Added</h3>&#39;;
 foreach ($added as $add) {
 echo $add.&#39;<br>&#39;;
}
 echo &#39;<h3>Files Removed</h3>&#39;;
 foreach ($removed as $remove) {
 echo $remove.&#39;<br>&#39;;
}

?>
</body>
</html>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template