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获得文件扩展名三法
Nov 25 PHP
Snoopy类使用小例子
Apr 15 PHP
php 显示指定路径下的图片
Oct 29 PHP
从PHP $_SERVER相关参数判断是否支持Rewrite模块
Sep 26 PHP
php防止sql注入示例分析和几种常见攻击正则表达式
Jan 12 PHP
php实现Session存储到Redis
Nov 11 PHP
php实现微信发红包
Dec 05 PHP
完美解决thinkphp唯一索引重复时出错的问题
Mar 31 PHP
可兼容php5与php7的cURL文件上传功能实例分析
May 11 PHP
Codeigniter里的无刷新上传的实现代码
Apr 14 PHP
php中目录操作opendir()、readdir()及scandir()用法示例
Jun 08 PHP
php操作redis数据库常见方法实例总结
Feb 20 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
如何正确配置Nginx + PHP
2016/07/15 PHP
PHP堆栈调试操作简单示例
2018/06/15 PHP
php抽象方法和普通方法的区别点总结
2019/10/13 PHP
让iframe自适应高度(支持XHTML,支持FF)
2007/07/24 Javascript
编写高性能的JavaScript 脚本的加载与执行
2010/04/19 Javascript
js滚动条回到顶部的代码
2011/12/06 Javascript
jQuery 过滤方法filter()选择具有特殊属性的元素
2014/06/15 Javascript
JS实现部分HTML固定页面顶部随屏滚动效果
2015/12/24 Javascript
JavaScript实现图片滑动切换的代码示例分享
2016/03/06 Javascript
jQuery如何封装输入框插件
2016/08/19 Javascript
使用jQuery的toggle()方法对HTML标签进行显示、隐藏的方法(示例)
2016/09/01 Javascript
解析NodeJs的调试方法
2016/12/11 NodeJs
Axios学习笔记之使用方法教程
2017/07/21 Javascript
JavaScript 复制对象与Object.assign方法无法实现深复制
2018/11/02 Javascript
微信小程序列表中item左滑删除功能
2018/11/07 Javascript
JointJS流程图的绘制方法
2018/12/03 Javascript
微信小程序图表插件wx-charts用法实例详解
2019/05/20 Javascript
ES6小技巧之代替lodash
2019/06/07 Javascript
ES6 async、await的基本使用方法示例
2020/06/06 Javascript
微信小程序实现上传照片代码实例解析
2020/08/04 Javascript
bpython 功能强大的Python shell
2016/02/16 Python
python批量获取html内body内容的实例
2019/01/02 Python
pyqt5中QThread在使用时出现重复emit的实例
2019/06/21 Python
Python3搭建http服务器的实现代码
2020/02/11 Python
python批量生成身份证号到Excel的两种方法实例
2021/01/14 Python
微信小程序之html5 canvas绘图并保存到系统相册
2019/06/20 HTML / CSS
澳大利亚排名第一的狂热牛仔品牌:ONETEASPOON
2018/11/20 全球购物
装饰活动策划方案
2014/02/11 职场文书
企业节能减排实施方案
2014/03/19 职场文书
关于护士节的演讲稿
2014/05/26 职场文书
大学专科自荐信
2014/06/17 职场文书
单位员工收入证明样本
2014/10/09 职场文书
中班教师个人总结
2015/02/05 职场文书
同学聚会通知书
2015/04/20 职场文书
2015年教师节主持词
2015/07/03 职场文书
单位病假条范文
2015/08/17 职场文书