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网站提速三大“软”招
Oct 09 PHP
php结合表单实现一些简单功能的例子
Jun 04 PHP
分享PHP header函数使用教程
Sep 05 PHP
php使用正则过滤js脚本代码实例
May 10 PHP
PHP程序员常见的40个陋习,你中了几个?
Nov 20 PHP
使用PHPExcel操作Excel用法实例分析
Mar 26 PHP
php中关于socket的系列函数总结
May 18 PHP
再Docker中架设完整的WordPress站点全攻略
Jul 29 PHP
PHP中SSO Cookie登录分析和实现
Nov 06 PHP
PHP使用反射机制实现查找类和方法的所在位置
Apr 22 PHP
PHPExcel笔记, mpdf导出
May 03 PHP
Yii框架连接mongodb数据库的代码
Jul 27 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 SPL标准库之接口(Interface)详解
2015/05/11 PHP
浅谈PHP拦截器之__set()与__get()的理解与使用方法
2016/10/18 PHP
PHP实现找出链表中环的入口节点
2018/01/16 PHP
php字符串截取函数mb_substr用法实例分析
2019/06/25 PHP
jquery 新手学习常见问题解决方法
2010/04/18 Javascript
js函数名与form表单元素同名冲突的问题
2014/03/07 Javascript
JavaScript中对象属性的添加和删除示例
2014/05/12 Javascript
深入理解JavaScript系列(44):设计模式之桥接模式详解
2015/03/04 Javascript
JavaScript通过事件代理高亮显示表格行的方法
2015/05/27 Javascript
js验证身份证号有效性并提示对应信息
2015/10/19 Javascript
三分钟带你玩转jQuery.noConflict()
2016/02/15 Javascript
easyui关于validatebox实现多重规则验证的方法(必看)
2017/04/12 Javascript
详解webpack异步加载业务模块
2017/06/23 Javascript
Node.JS使用Sequelize操作MySQL的示例代码
2017/10/09 Javascript
Javascript防止图片拉伸的自适应处理方法
2017/12/26 Javascript
vue-cli2.9.3 详细教程
2018/04/23 Javascript
关于Vue在ie10下空白页的debug小结
2018/05/02 Javascript
原生JS实现逼真的图片3D旋转效果详解
2019/02/16 Javascript
在 Vue 中使用 JSX 及使用它的原因浅析
2020/02/10 Javascript
python中的__init__ 、__new__、__call__小结
2014/04/25 Python
python采用getopt解析命令行输入参数实例
2014/09/30 Python
使用Python操作Elasticsearch数据索引的教程
2015/04/08 Python
django下创建多个app并设置urls方法
2020/08/02 Python
python 爬取小说并下载的示例
2020/12/07 Python
迪卡侬波兰体育用品商店:Decathlon波兰
2020/03/31 全球购物
EM Cosmetics官网:由彩妆大神Michelle Phan创办的独立品牌
2020/04/27 全球购物
什么是事务?为什么需要事务?
2012/01/09 面试题
Java中的异常处理机制的简单原理和应用
2013/04/27 面试题
小学教师的个人自我鉴定
2013/10/24 职场文书
硕士生工作推荐信
2014/03/07 职场文书
大学专科求职信
2014/07/02 职场文书
嘉宾邀请函
2015/01/31 职场文书
未中标通知书
2015/04/17 职场文书
SQL Server中交叉联接的用法详解
2021/04/22 SQL Server
分享CSS盒子模型隐藏的几种方式
2022/02/28 HTML / CSS
JavaScript前端面试组合函数
2022/06/21 Javascript