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 相关文章推荐
wordpress之wp-settings.php
Aug 17 PHP
IStream与TStream之间的相互转换
Aug 01 PHP
PHPMyAdmin 快速配置方法
May 11 PHP
关于php连接mssql:pdo odbc sql server
Jul 20 PHP
php中将html中的br换行符转换为文本输入中的换行符
Mar 26 PHP
解析php session_set_save_handler 函数的用法(mysql)
Jun 29 PHP
纯PHP代码实现支付宝批量付款
Dec 24 PHP
YII框架中搜索分页jQuery写法详解
Dec 19 PHP
php实现数据库的增删改查
Feb 26 PHP
php json转换相关知识(小结)
Dec 21 PHP
php curl获取https页面内容,不直接输出返回结果的设置方法
Jan 15 PHP
浅谈laravel中的关联查询with的问题
Oct 10 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
探讨:使用XMLSerialize 序列化与反序列化
2013/06/08 PHP
10 个经典PHP函数
2013/10/17 PHP
php ci框架中加载css和js文件失败的解决方法
2014/03/03 PHP
ThinkPHP中的常用查询语言汇总
2014/08/22 PHP
浅析php适配器模式(Adapter)
2014/11/25 PHP
PHP实现GIF图片验证码
2015/11/04 PHP
简单谈谈php延迟静态绑定
2016/01/26 PHP
老生常谈文本文件和二进制文件的区别
2017/02/27 PHP
父页面显示遮罩层弹出半透明状态的dialog
2014/03/04 Javascript
JavaScript对象数组的排序处理方法
2015/10/21 Javascript
实例解析jQuery中proxy()函数的用法
2016/05/24 Javascript
Jquery和JS获取ul中li标签的实现方法
2016/06/02 Javascript
JS中的hasOwnProperty()和isPrototypeOf()属性实例详解
2016/08/11 Javascript
浅谈js数据类型判断与数组判断
2016/08/29 Javascript
Bootstrap在线电子商务网站实战项目5
2016/10/14 Javascript
javascript常用经典算法详解
2017/01/11 Javascript
AngularJS前端页面操作之用户修改密码功能示例
2017/03/27 Javascript
AngularJS中$http的交互问题
2017/03/29 Javascript
微信web端后退强制刷新功能的实现代码
2018/03/04 Javascript
利用npm 安装删除模块的方法
2018/05/15 Javascript
vue悬浮可拖拽悬浮按钮的实例代码
2019/08/20 Javascript
详解Vue template 如何支持多个根结点
2020/02/10 Javascript
微信小程序实现电子签名并导出图片
2020/05/27 Javascript
Python的爬虫包Beautiful Soup中用正则表达式来搜索
2016/01/20 Python
python通过Windows下远程控制Linux系统
2018/06/20 Python
windows上安装python3教程以及环境变量配置详解
2019/07/18 Python
django框架使用方法详解
2019/07/18 Python
中国旅游网站:途牛旅游网
2019/09/29 全球购物
美国户外服装和装备购物网站:Outland USA
2020/03/22 全球购物
大学生第一学年自我鉴定
2014/09/12 职场文书
2014年小学重阳节活动策划方案
2014/09/16 职场文书
收款授权委托书
2014/10/02 职场文书
收款委托书
2014/10/14 职场文书
2015年医院创卫工作总结
2015/04/22 职场文书
化工厂员工工作总结
2015/10/15 职场文书
nginx中proxy_pass各种用法详解
2021/11/07 Servers