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 相关文章推荐
用PHP和MySQL保存和输出图片
Oct 09 PHP
PHP与SQL注入攻击[二]
Apr 17 PHP
初次接触php抽象工厂模式(Elgg)
Mar 21 PHP
PHP 年龄计算函数(精确到天)
Jun 07 PHP
php随机输出名人名言的代码
Oct 07 PHP
php获取本地图片文件并生成xml文件输出具体思路
Apr 27 PHP
php获取字段名示例分享
Mar 03 PHP
PHP中SimpleXML函数用法分析
Nov 26 PHP
php中print(),print_r(),echo()的区别详解
Dec 01 PHP
php中数据库连接方式pdo和mysqli对比分析
Feb 25 PHP
php+ajax实现带进度条的上传图片功能【附demo源码下载】
Sep 14 PHP
ThinkPHP框架表单验证操作方法
Jul 19 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移动文件指针ftell()、fseek()、rewind()函数总结
2014/11/18 PHP
功能强大的PHP POST提交数据类
2016/07/15 PHP
js下用gb2312编码解码实现方法
2009/12/31 Javascript
jquery关于页面焦点的定位(文本框获取焦点时改变样式 )
2010/09/10 Javascript
Jquery显示、隐藏元素以及添加删除样式
2013/08/09 Javascript
IE6-8中Date不支持toISOString的修复方法
2014/05/04 Javascript
jQuery中 prop() attr()使用详解
2015/05/19 Javascript
跟我学习javascript的Date对象
2015/11/19 Javascript
所见即所得的富文本编辑器bootstrap-wysiwyg使用方法详解
2016/05/27 Javascript
picLazyLoad 实现图片延时加载(包含背景图片)
2016/07/21 Javascript
JavaScript中关于for循环删除数组元素内容时出现的问题
2016/11/21 Javascript
JS中input表单隐藏域及其使用方法
2017/02/13 Javascript
如何以Angular的姿势打开Font-Awesome详解
2018/04/22 Javascript
代码分析vue中如何配置less
2018/09/28 Javascript
详解Vue2.0组件的继承与扩展
2018/11/23 Javascript
[46:44]VG vs TNC Supermajor小组赛B组败者组决赛 BO3 第一场 6.2
2018/06/03 DOTA
详解Python中的文件操作
2016/08/28 Python
使用python生成目录树
2018/03/29 Python
解决python测试opencv时imread导致的错误问题
2019/01/26 Python
Python 实现数据结构-循环队列的操作方法
2019/07/17 Python
详解Python绘图Turtle库
2019/10/12 Python
python中matplotlib实现随鼠标滑动自动标注代码
2020/04/23 Python
Python模拟伯努利试验和二项分布代码实例
2020/05/27 Python
idea2020手动安装python插件的实现方法
2020/07/17 Python
纯CSS3实现3D旋转书本效果
2016/03/21 HTML / CSS
html5定位并在百度地图上显示的示例
2014/04/27 HTML / CSS
详解canvas绘制多张图的排列顺序问题
2019/01/21 HTML / CSS
软件工程师面试题
2012/06/25 面试题
管理学专业个人求职信范文
2013/09/21 职场文书
初中英语课后反思
2014/04/25 职场文书
招股说明书范本
2014/05/06 职场文书
党的群众路线批评与自我批评范文
2014/10/16 职场文书
2015年暑假生活总结
2015/07/13 职场文书
健康教育主题班会
2015/08/14 职场文书
文艺部部长竞选稿
2015/11/21 职场文书
pytorch实现ResNet结构的实例代码
2021/05/17 Python