php 遍历目录,生成目录下每个文件的md5值并写入到结果文件中


Posted in PHP onDecember 12, 2016

php 遍历目录,生成目录下每个文件的md5值并写入到结果文件中

实例代码:

<?php
 
/** 
 * @author Administrator
 * 
 */
class TestGenerate {
  public static $appFolder = "";
  public static $ignoreFilePaths = array (
    "xxxx/xxx.php"
  );
  public static function start() {
    $AppPath = "E:\\myApp";
    TestGenerate::$appFolder = $AppPath;
    $destManifestPath = "E:\\temp2\\dest.md5.txt";
     
    // dest file handle
    $manifestHandle = fopen ( $destManifestPath, "w+" );
     
    // write header
    TestGenerate::writeMaifestHeader ( $manifestHandle );
     
    // write md5
    TestGenerate::traverse ( $AppPath, $manifestHandle );
     
    // write footer
    TestGenerate::writeMaifestFooter ( $manifestHandle );
     
    // close file
    fclose ( $manifestHandle );
  }
   
  /**
   * 遍历应用根目录下的文件,并生成对应的文件长度及md5信息
   *
   * @param unknown $AppPath
   *     应用根目录,如:xxx/xxx/analytics
   * @param string $destManifestPath
   *     生成的manifest文件存放位置的文件句柄
   */
  public static function traverse($AppPath, $manifestHandle) {
    if (! file_exists ( $AppPath )) {
      printf ( $AppPath . " does not exist!" );
      return;
    }
    if (! is_dir ( $AppPath )) {
      printf ( $AppPath . " is not a directory!" );
      return;
    }
    if (! ($dh = opendir ( $AppPath ))) {
      printf ( "Failure while read diectory!" );
      return;
    }
     
    // read files
    while ( ($file = readdir ( $dh )) != false ) {
      $subDir = $AppPath . DIRECTORY_SEPARATOR . $file;
       
      if ($file == "." || $file == "..") {
        continue;
      } else if (is_dir ( $subDir )) {
        // rescure
        TestGenerate::traverse ( $subDir, $manifestHandle );
      } else {
        // Sub is a file.
        TestGenerate::writeOneFieToManifest ( $subDir, $manifestHandle );
      }
    }
     
    // close dir
    closedir ( $dh );
  }
   
  /**
   * 写一个文件的md5信息到文件中
   *
   * @param unknown $filePath     
   * @param unknown $fileHandle      
   */
  public static function writeOneFieToManifest($filePath, $fileHandle) {
    if (! file_exists ( $filePath )) {
      continue;
    }
     
    $relativePath = str_replace ( TestGenerate::$appFolder . DIRECTORY_SEPARATOR, '', $filePath );
    $relativePath = str_replace ( "\\", "/", $relativePath );
     
    // ignore tmp directory
    if (strpos ( $relativePath, "tmp/" ) === 0) {
      return;
    }
     
    $fileSize = filesize ( $filePath );
    $fileMd5 = @md5_file ( $filePath );
     
    $content = "\t\t";
    $content .= '"';
    $content .= $relativePath;
    $content .= '"';
    $content .= ' => array("';
    $content .= $fileSize;
    $content .= '","';
    $content .= $fileMd5;
    $content .= '"),';
    $content .= "\n";
     
    if (! fwrite ( $fileHandle, $content )) {
      print ($filePath . " can not be written!") ;
    }
  }
   
  /**
   * 在manifes文件中写入头信息
   *
   * @param unknown $fileHandle      
   */
  public static function writeMaifestHeader($fileHandle) {
    $header = "<?php";
    $header .= "\n";
    $header .= "// This file is automatically generated";
    $header .= "\n";
    $header .= "namespace test;";
    $header .= "\n";
    $header .= "class MyFile {";
    $header .= "\n";
    $header .= "\tstatic \$allFiles=array(";
    $header .= "\n";
     
    if (! fwrite ( $fileHandle, $header )) {
      printf ( "Failure while write file header." );
    }
  }
   
  /**
   * 在manifes文件中写入尾部信息
   *
   * @param unknown $fileHandle      
   */
  public static function writeMaifestFooter($fileHandle) {
    $footer = "\t);";
    $footer .= "\n";
    $footer .= "}";
    $footer .= "\n";
     
    if (! fwrite ( $fileHandle, $footer )) {
      printf ( "Failure while write file header." );
    }
  }
}
 
// Start application
TestGenerate::start ();
 
?>

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

PHP 相关文章推荐
一个很方便的 XML 类!!原创的噢
Oct 09 PHP
用Socket发送电子邮件
Oct 09 PHP
一个可查询所有表的“通用”查询分页类
Oct 09 PHP
PHP+AJAX实现无刷新注册(带用户名实时检测)
Dec 02 PHP
PHP编码转换
Nov 05 PHP
php获取从百度搜索进入网站的关键词的详细代码
Jan 08 PHP
PHP 抽象方法与抽象类abstract关键字介绍及应用
Oct 16 PHP
php使用COPY函数更新配置文件的方法
Jun 18 PHP
PHP 的比较运算与逻辑运算详解
May 12 PHP
PHP实现的简单AES加密解密算法实例
May 29 PHP
PHP编程中的Session阻塞问题与解决方法分析
Aug 07 PHP
PHP使用pdo连接access数据库并循环显示数据操作示例
Jun 05 PHP
php+ajax+json 详解及实例代码
Dec 12 #PHP
解决微信授权回调页面域名只能设置一个的问题
Dec 11 #PHP
Zend Framework数据库操作方法实例总结
Dec 11 #PHP
smarty模板数学运算示例
Dec 11 #PHP
Zend Framework入门应用实例详解
Dec 11 #PHP
Zend Framework前端控制器用法示例
Dec 11 #PHP
Zend Framework路由器用法实例详解
Dec 11 #PHP
You might like
用PHP和ACCESS写聊天室(三)
2006/10/09 PHP
php查找任何页面上的所有链接的方法
2013/12/03 PHP
PHP中怎样防止SQL注入分析
2014/10/23 PHP
php自定义错误处理用法实例
2015/03/20 PHP
php实现的任意进制互转类分享
2015/07/07 PHP
php微信开发之带参数二维码的使用
2016/08/03 PHP
js取float型小数点后两位数的方法
2014/01/18 Javascript
无限树Jquery插件zTree的常用功能特性总结
2014/09/11 Javascript
利用jQuery中的ajax分页实现代码
2016/02/25 Javascript
js eval函数使用,js对象和字符串互转实例
2017/03/06 Javascript
discuz表情的JS提取方法分析
2017/03/22 Javascript
Vue 2中ref属性的使用方法及注意事项
2017/06/12 Javascript
JavaScript实现简单的双色球(实例讲解)
2017/07/31 Javascript
Vue中的字符串模板的使用
2018/05/17 Javascript
webpack自动打包和热更新的实现方法
2019/06/24 Javascript
Python中的高级数据结构详解
2015/03/27 Python
Python中利用原始套接字进行网络编程的示例
2015/05/04 Python
浅析Python中的赋值和深浅拷贝
2017/08/15 Python
python实现学生管理系统
2018/01/11 Python
Python应用库大全总结
2018/05/30 Python
Python面向对象程序设计构造函数和析构函数用法分析
2019/04/12 Python
Python 3.6 -win64环境安装PIL模块的教程
2019/06/20 Python
python 实现单通道转3通道
2019/12/03 Python
Pandas时间序列基础详解(转换,索引,切片)
2020/02/26 Python
详解Python yaml模块
2020/09/23 Python
Python操作Excel的学习笔记
2021/02/18 Python
python单例模式的应用场景实例讲解
2021/02/24 Python
英国山地公路自行车商店:Tweeks Cycles
2018/03/16 全球购物
毕业生就业推荐表自我鉴定
2014/03/20 职场文书
社区道德讲堂实施方案
2014/03/21 职场文书
升旗仪式演讲稿
2014/05/08 职场文书
经典演讲稿汇总
2014/05/19 职场文书
2015暑假打工实践报告
2015/07/13 职场文书
浅谈Python基础之列表那些事儿
2021/05/11 Python
漫画「日和酱的要求是绝对的」第3卷封面公开
2022/03/21 日漫
Java的Object类的九种方法
2022/04/13 Java/Android