利用php失去文件夹的md5校验值

WBOY
Release: 2016-06-13 13:25:28
Original
1007 people have browsed it

利用php得到文件夹的md5校验值

<?php /**
 * 工具文件
 * 最终目的在于比较两个文件夹的差异,
 * 
 * 但是本程序只用于得到一个目录的md5校验和,所以需要分别在不同机器上执行,然后比较md5校验和
 * 
 * 
 * 1)调用示例
 * php get_folder_md5.php /home/temp/2 
 * 
 * 2)我不想比.svn目录,所以程序中有$entry != ".svn",如果想彻底比较,请删除&& $entry != ".svn"
 * 3)
 * @author yyy
 */

//参数确定
if (count($argv) > 1 )
  $dir1 = del_postfix($argv[1]);
else 
  die('please input dir');



$global_str = md5('1');  
  
//检查第一个路径有,后者没有或错误的方法。
get_folder_md5($dir1);
echo "===========================================================\n";

echo $global_str;
echo "\n";


/**
 * 去除路径末尾的/,并确保是绝对路径
 *
 * @param unknown_type $dir
 * @return unknown
 */
function del_postfix($dir)
{
    if (!preg_match('#^/#', $dir)) {
        throw new Exception('参数必须是绝对路径');
    }
    $dir = preg_replace('#/$#', '', $dir);
    return $dir;
}




/**
 * 递归调用获取md5
 *
 * @param string $dir1        路径1,是标准
 */
function get_folder_md5($dir1){
    global $global_str;
    if (is_dir($dir1)) {
        $arr =  scandir($dir1);
        foreach ($arr as $entry) {
            if (($entry != ".") && ($entry != "..")  && ($entry != ".svn")){
                $new = $dir1."/".$entry; //$new是完整文件名或文件夹名
                //如果不想显示文件名可以注释下面这句
                echo $entry ."\n";
                $global_str = md5($global_str . $entry);
                if(is_dir($new)) {
                    get_folder_md5($new) ;
                } else { 
                    $global_str = md5($global_str . md5_file($new) );
                }
            }
        }
    }
}

Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!