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 相关文章推荐
destoon公司主页模板风格的添加方法
Jun 20 PHP
Laravel 4 初级教程之安装及入门
Oct 30 PHP
摘自织梦CMS的HTTP文件下载类
Aug 08 PHP
yii数据库的查询方法
Dec 28 PHP
Windows2003下php5.4安装配置教程(Apache2.4)
Jun 30 PHP
PHP数据库表操作的封装类及用法实例详解
Jul 12 PHP
PHP中字符串长度的截取用法示例
Jan 12 PHP
php 字符串中是否包含指定字符串的多种方法
Apr 12 PHP
PHP设计模式之模板方法模式实例浅析
Dec 20 PHP
php中pcntl_fork创建子进程的方法实例
Mar 14 PHP
php扩展开发入门demo示例
Sep 23 PHP
Thinkphp集成抖音SDK的实现方法
Apr 28 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
discuz论坛 用户登录 后台程序代码
2008/11/27 PHP
一个简单php扩展介绍与开发教程
2010/08/19 PHP
基于jquery的无限级联下拉框js插件
2011/10/29 Javascript
js简单实现用户注册信息的校验代码
2013/11/15 Javascript
jquery图片切换实例分析
2015/04/15 Javascript
Jquery常用的方法汇总
2015/09/01 Javascript
jQuery zclip插件实现跨浏览器复制功能
2015/11/02 Javascript
jQuery操作Table技巧大汇总
2016/01/23 Javascript
Zabbix添加Node.js监控的方法
2016/10/20 Javascript
node+express制作爬虫教程
2016/11/11 Javascript
Html5+jQuery+CSS制作相册小记录
2016/12/30 Javascript
AngularJS实现tab选项卡的方法详解
2017/07/05 Javascript
使用layui实现树形结构的方法
2019/09/20 Javascript
详解小程序云开发攻略(解决最棘手的问题)
2019/09/30 Javascript
JS实现简单随机3D骰子
2019/10/24 Javascript
[01:32:10]NAVI vs VG Supermajor 败者组 BO3 第一场 6.5
2018/06/06 DOTA
python读文件逐行处理的示例代码分享
2013/12/27 Python
Python实现根据指定端口探测服务器/模块部署的方法
2014/08/25 Python
深入解析Python中的descriptor描述器的作用及用法
2016/06/27 Python
Python 3中print函数的使用方法总结
2017/08/08 Python
简单了解Python中的几种函数
2017/11/03 Python
对TensorFlow中的variables_to_restore函数详解
2018/07/30 Python
解决python中遇到字典里key值为None的情况,取不出来的问题
2018/10/17 Python
Python3 JSON编码解码方法详解
2019/09/06 Python
python模拟哔哩哔哩滑块登入验证的实现
2020/04/24 Python
Spartoo芬兰:欧洲最大的网上鞋店
2016/08/28 全球购物
护理专科自荐书范文
2014/02/18 职场文书
活动策划求职信模板
2014/04/21 职场文书
物资采购方案
2014/06/12 职场文书
建筑工地大门标语
2014/06/18 职场文书
防邪知识进家庭活动方案
2014/08/26 职场文书
大学生入党积极分子自我评价
2014/09/20 职场文书
2014年平安建设工作总结
2014/11/19 职场文书
实习单位推荐信
2015/03/27 职场文书
2019年教师入党申请书
2019/06/27 职场文书
使用Python解决图表与画布的间距问题
2022/04/11 Python