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
php5.2时间相差8小时
Jan 15 PHP
PHP性能优化工具篇Benchmark类调试执行时间
Dec 06 PHP
谨慎使用PHP的引用原因分析
Sep 06 PHP
php中定时计划任务的实现原理
Jan 08 PHP
PHP的curl实现get,post和cookie(实例介绍)
Jun 17 PHP
AJAX的跨域访问-两种有效的解决方法介绍
Jun 22 PHP
以文件形式缓存php变量的方法
Jun 26 PHP
CodeIgniter与PHP5.6的兼容问题
Jul 16 PHP
46 个非常有用的 PHP 代码片段
Feb 16 PHP
由php中字符offset特征造成的绕过漏洞详解
Jul 07 PHP
PHP实现执行外部程序的方法详解
Aug 17 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
ThinkPHP之N方法实例详解
2014/06/20 PHP
php通过baihui网API实现读取word文档并展示
2015/06/22 PHP
PHP合并数组+号和array_merge的区别
2015/06/25 PHP
thinkPHP实现的联动菜单功能详解
2017/05/05 PHP
php获取ajax的headers方法与内容实例
2017/12/27 PHP
增强的 JavaScript 的 trim 函数的代码
2007/08/13 Javascript
JS保留两位小数 四舍五入函数的小例子
2013/11/20 Javascript
javascript面向对象之定义成员方法实例分析
2015/01/13 Javascript
介绍JavaScript的一个微型模版
2015/06/24 Javascript
JS实现自动切换文字的导航效果代码
2015/08/27 Javascript
AngularJS手动表单验证
2016/02/01 Javascript
基于jQuery实现带动画效果超炫酷的弹出对话框(附源码下载)
2016/02/22 Javascript
JavaScript实现简单的文本逐字打印效果示例
2018/04/12 Javascript
Vue 报错TypeError: this.$set is not a function 的解决方法
2018/12/17 Javascript
小程序server请求微信服务器超时的解决方法
2019/05/21 Javascript
[46:44]DOTA2-DPC中国联赛 正赛 Ehome vs PSG.LGD BO3 第二场 3月7日
2021/03/11 DOTA
Python变量作用范围实例分析
2015/07/07 Python
Python常用知识点汇总
2016/05/08 Python
Python获取文件所在目录和文件名的方法
2017/01/12 Python
Python 解决中文写入Excel时抛异常的问题
2018/05/03 Python
python腾讯语音合成实现过程解析
2019/08/01 Python
python读取大文件越来越慢的原因与解决
2019/08/08 Python
python3连接mysql获取ansible动态inventory脚本
2020/01/19 Python
利用python对excel中一列的时间数据更改格式操作
2020/07/14 Python
Notino希腊:购买香水和美容产品
2019/07/25 全球购物
罗技美国官网:Logitech美国
2020/01/22 全球购物
大学生未来职业生涯规划书
2014/02/15 职场文书
中文教师求职信
2014/02/22 职场文书
计算机应届毕业生自荐信范文
2014/02/23 职场文书
我爱家乡演讲稿
2014/09/12 职场文书
2014离婚协议书范文两篇
2014/09/15 职场文书
教师专业技术工作总结2015
2015/05/13 职场文书
MySQL之高可用集群部署及故障切换实现
2021/04/22 MySQL
微信小程序基础教程之echart的使用
2021/06/01 Javascript
el-form每行显示两列底部按钮居中效果的实现
2022/08/05 HTML / CSS
MySQL生成千万测试数据以及遇到的问题
2022/08/05 MySQL