比较文件并创建记事本以判断 php 中的哪一行不同


Comparing files and creating notepad to tell which line is different in php

在这段代码之后,我被困了一段时间。我在使用 php 编码方面不是那么新,但我需要这方面的帮助。

我有两个目录,其中包含一个以上的文件,它可能以文本或 xml 的形式出现。假设目录 1 今天被填充,目录 2 将被填充 tom。两个目录具有相同的文件数以及相同的名称和顺序。

我想要的是一个比较工具,它将对每个文件进行映射,但当然我需要比较那些名称相似的文件,然后如果任何一个文件有差异或更新,它将创建一个记事本,它将写入文件名,它们有差异的行。

我来到了这部分,但我卡住了,我需要你们的帮助。

谢谢。

<?php
$dirA = glob('C:'Users'aganda88'Desktop'testing docs'testingdocs1'*');
$dirB = glob('C:'Users'aganda88'Desktop'testing docs'testingdocs2'*');
//Checking that the files are the same
foreach($dirA as $fileName) {
    // the file exists in the other folder as well with the same name
    if ($exists = array_search($fileName, $dirB) !== false) {
       // it exists!
    if (md5_file($fileName) !== md5_file($dirB[$exists])) {
        // The files are not identical so i need to create a text file to show what line is not the same
        copy($fileName, $dirB[$exists]);
        /* problem here is you didn't specify which in your requirements */
    }
} else {
    // it doesn't (what to do here? you didn't specify!)
}
}
// compare the other way
foreach($dirB as $fileName) {
// does the file exist in the other directory?
if ($exists = array_search($fileName, $dirA) !== false) {
    // it exists!
    if (md5_file($fileName) !== md5_file($dirA[$exists])) {
        copy($fileName, $dirA[$exists]);
    }
} else {
    // it doesn't (what to do here? you didn't specify!)
}
}
?>

获取每个目录中所有文件的列表

您可以使用glob获取每个目录中的文件列表...

$dirA = glob('C:'Users'aganda88'Desktop'testing docs'testingdocs1'*');
$dirB = glob('C:'Users'aganda88'Desktop'testing docs'testingdocs2'*');

检查文件是否相同

您可以对匹配的文件名执行简单的 md5 校验和,以确定它们是否相同。

foreach($dirA as $fileName) {
    // does the file exist in the other directory?
    if ($exists = array_search($fileName, $dirB) !== false) {
        // it exists!
        if (md5_file($fileName) !== md5_file($dirB[$exists])) {
            // The files aren't identical so one of them needs updated
            copy($fileName, $dirB[$exists]);
            /* problem here is you didn't specify which in your requirements */
        }
    } else {
        // it doesn't (what to do here? you didn't specify!)
    }
}
// compare the other way
foreach($dirB as $fileName) {
    // does the file exist in the other directory?
    if ($exists = array_search($fileName, $dirA) !== false) {
        // it exists!
        if (md5_file($fileName) !== md5_file($dirA[$exists])) {
            copy($fileName, $dirA[$exists]);
        }
    } else {
        // it doesn't (what to do here? you didn't specify!)
    }
}

更新文件

您的要求中有一部分您没有明确定义行为......您更新哪个文件?因为虽然可能已经对C:'Users'aganda88'Desktop'testing docs'testingdocs1'test1.txt进行了更改,但这些要求不会区分您用C:'Users'aganda88'Desktop'testing docs'testingdocs2'test1.txt覆盖C:'Users'aganda88'Desktop'testing docs'testingdocs1'test1.txt还是用C:'Users'aganda88'Desktop'testing docs'testingdocs1'test1.txt覆盖C:'Users'aganda88'Desktop'testing docs'testingdocs2'test1.txt......即文件不相同的事实并没有指定哪个应该是,哪个应该是目标。没有这个,你知道它们不一样并不重要......