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 相关文章推荐
1.PHP简介
Oct 09 PHP
php开发环境配置记录
Jan 14 PHP
JpGraph php柱状图使用介绍
Aug 23 PHP
关于crontab的使用详解
Jun 24 PHP
关于使用key/value数据库redis和TTSERVER的心得体会
Jun 28 PHP
php的ajax简单实例
Feb 27 PHP
ThinkPHP整合百度Ueditor图文教程
Oct 21 PHP
php中stdClass的用法分析
Feb 27 PHP
PHP生成随机字符串(3种方法)
Sep 25 PHP
Smarty模板简单配置与使用方法示例
May 23 PHP
PHP基于接口技术实现简单的多态应用完整实例
Apr 26 PHP
PHP基于ICU扩展intl快速实现汉字转拼音及按拼音首字母分组排序的方法
May 03 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获取input输入框中的值去数据库比较显示出来
2016/11/16 PHP
php使用自定义函数实现汉字分割替换功能示例
2017/01/30 PHP
PHP 返回数组后处理方法(开户成功后弹窗提示)
2017/07/03 PHP
javascript removeChild 使用注意事项
2009/04/11 Javascript
JavaScript获取和设置CheckBox状态的简单方法
2013/07/05 Javascript
js判断浏览器类型为ie6时不执行
2014/06/15 Javascript
javascript对中文按照拼音排序代码
2014/08/20 Javascript
jquery删除指定子元素代码实例
2015/01/13 Javascript
JavaScript实现表格点击排序的方法
2015/05/11 Javascript
jQuery中(function($){})(jQuery)详解
2015/07/15 Javascript
浅谈Vue.js应用的四种AJAX请求数据模式
2017/08/30 Javascript
Vue官方文档梳理之全局配置
2017/11/22 Javascript
微信小程序表单验证功能完整实例
2017/12/01 Javascript
解决Vue axios post请求,后台获取不到数据的问题方法
2018/08/11 Javascript
详解react-refetch的使用小例子
2019/02/15 Javascript
详细介绍解决vue和jsp结合的方法
2020/02/06 Javascript
vue动态合并单元格并添加小计合计功能示例
2020/11/26 Vue.js
Python3通过Luhn算法快速验证信用卡卡号的方法
2015/05/14 Python
理解Python中的With语句
2016/03/18 Python
Python数据类型详解(二)列表
2016/05/08 Python
Python回文字符串及回文数字判定功能示例
2018/03/20 Python
Python使用 Beanstalkd 做异步任务处理的方法
2018/04/24 Python
Python中的Socket 与 ScoketServer 通信及遇到问题解决方法
2019/04/01 Python
python操作openpyxl导出Excel 设置单元格格式及合并处理代码实例
2019/08/27 Python
Python生成个性签名图片获取GUI过程解析
2019/12/16 Python
CSS3 实现弹跳的小球动画
2020/10/26 HTML / CSS
精油和天然健康美容产品:Art Naturals
2018/01/27 全球购物
什么是虚拟内存?虚拟内存有什么优势?
2012/02/19 面试题
婚庆公司计划书
2014/09/15 职场文书
2014年最新版离婚协议书范本
2014/11/25 职场文书
难以忽视的真相观后感
2015/06/05 职场文书
2019年描写人生经典诗句大全
2019/07/08 职场文书
确保减税降费落地生根,用实实在在措施
2019/07/19 职场文书
python神经网络编程之手写数字识别
2021/05/08 Python
Python实现仓库管理系统
2022/05/30 Python
详解SQL报错盲注
2022/07/23 SQL Server