|
|
|
เอา PHP คอมแพร 2 ไพล์ว่าเหมือนกันไหม คือผมได้งานจากหัวหน้ามา เกี่ยวกับการเปรียบเทียบไพล์ครับ |
|
|
|
|
|
|
|
ต้องการเช็คจากอะไรครับ
ข้อมูลในไฟล์
วันที่สร้างไฟล์
วันที่อัพเดทไฟล์
ชื่อไฟล์
หรือทั้งหมด (เหมือนกับ ก๊อปปี้กันมา)
|
|
|
|
|
Date :
2010-11-03 17:13:02 |
By :
adaaugusta |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองศึกษาจาก Class นี้ดูครับ
ClassToCompareFiles.inc.php
<?Php
/**
* Class to compare two similar (text) files
* It returns the output in two iframes and highlights the differences
*
*
* @author Rochak Chauhan
*/
Class ClassToCompareFiles {
public $cnt1 = 0;
public $cnt2 = 0;
/**
* Function to create an array from file contents (text)
*
* @param $fileContents string - File contents
*
* @return array - array of lines of the file contents
*/
private function filetoArray($fileContents) {
return file($fileContents);
}
/**
* Function to compare two file contents
*
* @param $file1 string - path of the first file
* @param $file2 string - path of the second file
*
*/
public function compareFiles($file1, $file2) {
$file1ReturnArray = Array();
$file2ReturnArray = Array();
$mainFileArray = file($file1);
$duplicateFileArray = file($file2);
$linesInMainFile = count($mainFileArray);
$linesInDuplicateFile = count($duplicateFileArray);
// Main login to highlight lines
if( $linesInMainFile >= $linesInDuplicateFile ) {
for($i=0; $i<$linesInDuplicateFile; $i++) {
if($mainFileArray[$i] == $duplicateFileArray[$i]) {
$file1ReturnArray[] = "<font style='background-color:#ccddff'>".htmlentities($mainFileArray[$i])."</font>";
$file2ReturnArray[] = "<font style='background-color:#ffccdd'>".htmlentities($duplicateFileArray[$i])."</font>";
$this->cnt1++;
}
else{
$file1ReturnArray[] = "<font style='background-color:#ffccdd'>".htmlentities($mainFileArray[$i])."</font>";
$file2ReturnArray[] = "<font style='background-color:#ccddff'>".htmlentities($duplicateFileArray[$i])."</font>";
$this->cnt2++;
}
}
}
else {
for($i=0; $i<$linesInMainFile; $i++) {
if($mainFileArray[$i] == $duplicateFileArray[$i] ) {
$file1ReturnArray[] = "<font style='background-color:#ccddff'>".htmlentities($mainFileArray[$i])."</font>";
$file2ReturnArray[] = "<font style='background-color:#ffccdd'>".htmlentities($duplicateFileArray[$i])."</font>";
$this->cnt1++;
}
else {
$file1ReturnArray[] = "<font style='background-color:#ffccdd'>".htmlentities($mainFileArray[$i])."</font>";
$file2ReturnArray[] = "<font style='background-color:#ccddff'>".htmlentities($duplicateFileArray[$i])."</font>";
$this->cnt2++;
}
}
}
$this->createIFrames($file1ReturnArray, $file2ReturnArray);
}
/**
* Function to create left and right iframes to display the comparison between files.
*
* @param $file1Contents array - Modified/ Highlighted contents of file1
* @param $file2Contents array - Modified/ Highlighted contents of file2
*
*/
public function createIFrames($file1Contents, $file2Contents) {
// prepare file1 to display
$openFile1 = fopen('file1.html', 'w');
$html1Code = "<html> <body bgcolor='#ccddff'>";
fwrite($openFile1, $html1Code);
for($i=0; $i<count($file1Contents); $i++) {
$file1 = "<PRE><font style='background-color:#ffccdd'><b>".($i+1). "</b></font>".$file1Contents[$i]."</PRE>";
fwrite($openFile1, $file1);
}
fwrite($openFile1, "</body></html>");
fclose($openFile1);
// prepare file2 to display
$openFile2 = fopen('file2.html', 'w');
$html2Code = "<html> <body bgcolor='#ffccdd'>";
fwrite($openFile2, $html2Code);
for($j=0; $j<count($file2Contents); $j++) {
$file2 = "<PRE><font style='background-color:#ffccdd'><b>".($j+1). "</b></font>".$file2Contents[$j]."</PRE>";
fwrite($openFile2, $file2);
}
fwrite($openFile2, "</body></html>");
fclose($openFile2);
}
}
?>
Example.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title> Test for Rochak Chauhan's CompareFiles Class </title>
<script language="JavaScript" type="text/javascript">
<!--
function onSubmit() {
}
//-->
</script>
</head>
<body>
<center>
<font face="verdana" size="4" color="green" >Please Select the two files, which are to be compared.</font>
</center>
<br/>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<table border="0" width="100%" cellspacing="0" cellpadding="0" style="border-top: medium solid #000000;border-right: medium solid #000000;border-left: medium solid #000000;border-bottom: medium solid #000000">
<tr >
<td width="50%" align="center" bgcolor="#ccddff">
<font face="verdana" size="3" ><b>Select the main file: </b></font>
<input type="file" name="mainFile"/>
</td>
<td width="50%" align="center" bgcolor="#ffccdd">
<font face="verdana" size="3" ><B>Select the file to be compared: </b></font>
<input type="file" name="fileToCompare"/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<br/>
<input type="submit" value="Start Comparison" name="submitButton" onclick="return onSubmit()"/>
<br/>
</td>
</tr>
<tr>
<td colspan='2'>
<br />
</td>
</tr>
</table>
</form>
<?Php
if( isset($_POST['submitButton'])) {
require_once('ClassToCompareFiles.inc.php');
$compareFiles = new ClassToCompareFiles;
// File paths of the two files
$file1 = $_FILES['mainFile']['tmp_name'];
$file2 = $_FILES['fileToCompare']['tmp_name'];
$file1Contents = file($file1);
$file2Contents = file($file2);
$compareFiles->compareFiles($file1, $file2);
?>
<center><font face="verdana" size="6" ><B> Comparison Result </b></font> </center> <br />
<?php
echo "<center><font face='verdana' size='3' color='green'><b>Number of Similar line(s): ". $compareFiles->cnt1."</font><br />";
echo "<BR /><font face='verdana' size='3' color='red'>Number of Different line(s): ". $compareFiles->cnt2."</font></center></b><br />";
?>
<table border="1" style="width:100%;height:400px" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="#ccddff" style="width:50%" >
<iframe src="file1.html" width="100%" height="400" frameborder='0' ></iframe>
</td>
<td bgcolor="#ffccdd" style="width:50%" >
<iframe src="file2.html" width="100%" height="400" frameborder='0' ></iframe>
</td>
</tr>
</table>
<?php
}
?>
</body>
</html>
|
|
|
|
|
Date :
2010-11-03 17:29:29 |
By :
kerb |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แบบนี้ไม่ได้เหรอคับ
if (file_get_content('file1.xxx') === file_get_content('file2.xxx'))
|
|
|
|
|
Date :
2010-11-03 17:56:20 |
By :
pjgunner.com |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|