PHP比较两个文件夹以查找丢失的文件


PHP compare 2 folders to find missing files

我有两个图像文件夹original/和thumbs/,但文件数量不同(thumbs/较少),所以我需要找到哪些图像丢失了,文件名完全相同,没有拇指前缀的IE,所以正如标题所说,我如何比较两个文件夹并输出丢失图像的列表?

更新:

所有的答案似乎都没有达到我想要的效果,但我想出了这个非常粗糙的答案

不建议使用!!

我们给身体一个黑暗的背景,然后风格img与明亮的突出颜色:

<style>
    body{background:#000;}
    img {height:10px;width:10px;background:pink;border:1px solid #ccc;padding:10px;margin:5px;}
</style>

PHP:

// Read dir ...
        $images = glob('original/{*.jpg,*.gif,*.png}', GLOB_BRACE);
// throw out a foreach ...
        foreach($images as $image) 
         {
// using str_replace switch folders 
          $image  = str_replace('original/', 'thumbs/', $image);
// echo result 
          $result = '<img src="' . $image . '" alt="m" />';
          echo $result;
}

经过长时间的等待和近乎扼杀你的系统后,所有的图像都会显示出来,现在你只需查看每个图像,就可以找到丢失的图像。

请在此处查看结果:http://s12.postimage.org/5kcsvs48r/missing.jpg

就像我说的,这是一种非常粗糙的方法,而且非常占用服务器,如果只有几张图片,可能还可以,但你很容易通过阅读找到它们,但我有8000多张图片要看,无论如何,就像我之前说的那样,这是不可取的,但我想我会分享我的答案,不管是好是坏。

开始的方法:

$original = array_map('basename', glob('original/*.{jpg,gif,png}', GLOB_BRACE));
$thumbs = array_map('basename', glob('thumbs/*.{jpg,gif,png}', GLOB_BRACE));
$missing_thumbs = array_diff($original, $thumbs);
$missing_original = array_diff($thumbs, $original);

编辑说明:我错过了array_diff()在这里不起作用,因为glob()数组包含路径名。。。使用basename()映射进行了修补,现在它将按预期工作。

php.net有很多关于如何通过目录的文档和讨论(请参见:scandir()readdir()glob()等等)。

示例1

本例读取一个平面目录(没有子目录)并对它们进行比较,最后将结果留在数组中。

$original = glob('path/to/original/*.*');
$thumbs   = glob('path/to/thumbs/*.*');
$missing = array('original' => array(), 'thumbs' => array());
$missing['original']   = array_diff($thumbs, $original);
$missing['thumbs'] = array_diff($original, $thumbs);
print_r($missing); // Prints out a sorted array of the missing files for each dir

此示例的问题是,如果两个文件夹中都有子文件夹。为了处理这个问题,您必须使用如下递归结构。


示例2

此示例使用递归通过thumbs/original/的子目录的所有来查找丢失文件的all

/* Returns an associative array of all files under a directory,
including those inside of sub-directories, using recursion */
function scan($path) {
  $dir = opendir($path);
  
  while(false !== ($f = readdir($handle)))
    if(is_dir($f))
        $files[$f] = scan($path.$f.'/');
      else
        $files[$f] = $f;
    
    return $files;
  }
$original = scan('path/to/original/');
$thumbs = scan('path/to/thumbs/');
$missing['thumbs'] = array_diff($original, $thumbs);
$missing['original'] = array_diff($thumbs, $original);
print_r($missing);
print_r($original);
print_r($thumbs);

以下内容摘自www.php.net/opendir/:

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo "filename: $file : filetype: " . filetype($dir . $file) . "'n";
        }
        closedir($dh);
    }
}

您可以对两个目录执行此操作,并将文件名放置在单独的数组中,而不是回显它们。然后比较数组以查找丢失的文件。

$originalsDir = 'path';
$originalsArray = array();
if ($handle = opendir($originalsDir)) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry == '.' || $entry == '..') {
            continue;
        }
        $originalsArray[] = $entry;
    }
    closedir($handle);
} else {
    die('Could not open originals dir: ' . $originalsDir);
}
$thumbsDir = 'path';
$thumbsArray = array();
if ($handle = opendir($thumbsDir)) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry == '.' || $entry == '..') {
            continue;
        }
        $thumbsArray[] = $entry;
    }
    closedir($handle);
} else {
    die('Could not open thumbs dir: ' . $thumbsDir);
}
$missingThumbs = array_diff($originalsArray, $thumbsArray);

请参阅array_diff()、opendir()、readdir()

$dir_files1 = array();
$dir_files2 = array();
//Do this twice, Once for each directory
if ($handle = opendir('/path/to/files1')) {
    echo "Directory handle: $handle'n";
    echo "Entries:'n";
    /* This is the correct way to loop over the directory. */
    while (false !== ($entry = readdir($handle))) {
        $dir_files1[] = $entry;
    }
}
//Then compare the two arrays with eachother
foreach($dir_files1 as $d1)
{
 $match =false;
 foreach($dir_files2 as $d2)
 {
    if($d1==$d2)
     $match=true;
 }
 if(!$match)
   do_something();
}