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 相关文章推荐
ADODB的数据库封包程序库
Dec 31 PHP
PHP在字符串中查找指定字符串并删除的代码
Oct 02 PHP
PHP中用接口、抽象类、普通基类实现“面向接口编程”与“耦合方法”简述
Mar 23 PHP
PHP __autoload()方法真的影响性能吗?
Mar 30 PHP
基于PHP magic_quotes_gpc的使用方法详解
Jun 24 PHP
php导出word格式数据的代码实例
Nov 25 PHP
讲解WordPress中用于获取评论模板和搜索表单的PHP函数
Dec 28 PHP
如何使用php等比例缩放图片
Oct 12 PHP
Yii框架数据模型的验证规则rules()被执行的方法
Dec 02 PHP
php删除txt文件指定行及按行读取txt文档数据的方法
Jan 30 PHP
tp5(thinkPHP5)框架连接数据库的方法示例
Dec 24 PHP
Laravel5.5 视图 - 创建视图和数据传递示例
Oct 21 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中memcache的应用
2013/06/18 PHP
Yii实现多按钮保存与提交的方法
2014/12/03 PHP
PHP通过API获取手机号码归属地
2015/05/28 PHP
zen cart实现订单中增加paypal中预留电话的方法
2016/07/12 PHP
php实现产品加入购物车功能(1)
2020/07/23 PHP
IE与FireFox的兼容性问题分析
2007/04/22 Javascript
XML的代替者----JSON
2007/07/21 Javascript
javascript 网页跳转的方法
2008/12/24 Javascript
Jquery 设置标题的自动翻转
2009/10/03 Javascript
Jqyery中同等与js中windows.onload的应用
2011/05/10 Javascript
JS记录用户登录次数实现代码
2014/01/15 Javascript
js判断ie版本号的简单实现代码
2014/03/05 Javascript
JavaScript判断一个字符串是否包含指定子字符串的方法
2015/03/18 Javascript
javascript实现在指定元素中垂直水平居中
2015/09/13 Javascript
基于node实现websocket协议
2016/04/25 Javascript
vue组件实例解析
2017/01/10 Javascript
jQuery表单设置值的方法
2017/06/30 jQuery
Vue的MVVM实现方法
2017/08/16 Javascript
angular4自定义表单控件[(ngModel)]的实现
2018/11/23 Javascript
vant IndexBar实现的城市列表的示例代码
2019/11/20 Javascript
js数组中去除重复值的几种方法
2020/08/03 Javascript
[53:21]2014 DOTA2国际邀请赛中国区预选赛5.21 DT VS LGD-CDEC
2014/05/22 DOTA
详解Python中用于计算指数的exp()方法
2015/05/14 Python
python+matplotlib绘制饼图散点图实例代码
2018/01/20 Python
对python中两种列表元素去重函数性能的比较方法
2018/06/29 Python
Python实现使用request模块下载图片demo示例
2019/05/24 Python
基于Python3.6中的OpenCV实现图片色彩空间的转换
2020/02/03 Python
windows10 pycharm下安装pyltp库和加载模型实现语义角色标注的示例代码
2020/05/07 Python
Python利用matplotlib绘制折线图的新手教程
2020/11/05 Python
英国网上超市:Ocado
2020/03/05 全球购物
Marlies Dekkers内衣荷兰官方网店:荷兰奢侈内衣品牌
2020/03/27 全球购物
优秀的教师个人的中文求职信
2013/09/21 职场文书
财务工作者先进事迹材料
2014/01/17 职场文书
双方协议书
2014/04/22 职场文书
企业读书活动总结
2014/06/30 职场文书
体育教师个人工作总结
2015/02/09 职场文书