比较文件并创建记事本,以区分php中的哪一行不同

比较文件并创建记事本,以区分php中的哪一行不同,php,Php,在这之后我被困了一段时间。我在使用php编程方面不是很新,也不是很老,但我需要这方面的帮助 我有两个目录,其中有一个以上的文件,它可以是文本或xml。 假设目录1今天被填充,目录2将被填充。这两个目录有相同数量的文件,相同的名称和顺序 我想要的是一个比较工具,它可以比较每一个文件,但我当然需要比较那些名称相似的文件,然后如果其中任何一个文件有差异或更新,它会创建一个记事本,记录文件名,它们有差异的行 我来这一部分,但我卡住了,我需要你们的帮助 谢谢 <?php $dirA = glob('

在这之后我被困了一段时间。我在使用php编程方面不是很新,也不是很老,但我需要这方面的帮助

我有两个目录,其中有一个以上的文件,它可以是文本或xml。 假设目录1今天被填充,目录2将被填充。这两个目录有相同数量的文件,相同的名称和顺序

我想要的是一个比较工具,它可以比较每一个文件,但我当然需要比较那些名称相似的文件,然后如果其中任何一个文件有差异或更新,它会创建一个记事本,记录文件名,它们有差异的行

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

谢谢

<?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!)
}
}
?>
获取每个目录中所有文件的列表
您可以使用获取每个目录中的文件列表

$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\agand88\Desktop\testing docs\testing docs\testingdocs 1\test1.txt
进行了更改,但这些要求并不区分您是否将
C:\Users\agand88\Desktop\testing docs\testingdocs 1.txt
覆盖到
C:\Users\agand88\Desktop\testing docs\testingdocs 2\test1.txt
C:\Users\aganda88\Desktop\testing docs\testingdocs2\test1.txt
with
C:\Users\aganda88\Desktop\testing docs\testingdocs1\test1.txt
。。。i、 e.文件不相同的事实并没有指定哪个文件应该是,哪个文件应该是目标。如果你不知道它们是否相同,那就无关紧要了。

我来处理else,因为我有东西给else,如果它转到else的话