【问题标题】:PHP compare 2 folders to find missing filesPHP比较2个文件夹以查找丢失的文件
【发布时间】:2012-07-06 16:10:06
【问题描述】:

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

更新:

似乎没有一个答案符合我的要求,但我想出了这个非常粗略的答案

不建议使用!!

我们给 body 一个深色背景,然后用明亮的突出颜色为 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 张图片要浏览,无论如何,就像我一样之前说过这是不可取的,但我认为无论如何我都会分享我的答案。

【问题讨论】:

  • 你试过 opendir();获取目录中的文件列表?
  • 或者至少,到目前为止你尝试过什么?
  • 我已经更新了我的答案并修复了关于您的评论的代码。 :)

标签: php compare


【解决方案1】:

一种开始的方式:

$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() 映射,现在它可以按预期工作.

【讨论】:

    【解决方案2】:

    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查找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);
    

    【讨论】:

      【解决方案3】:

      以下摘自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);
          }
      }
      

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

      【讨论】:

        【解决方案4】:
        $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()

        【讨论】:

          【解决方案5】:
          $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();
          }
          

          【讨论】:

            猜你喜欢
            • 2012-06-20
            • 1970-01-01
            • 2022-07-27
            • 2012-08-06
            • 1970-01-01
            • 2015-12-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多