php遍历目录,生成目录下每个文件的md5值并写入到结果文件中
时间:2016-03-10
本文章向大家介绍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 ();
?>
- Docker监控方案(TIG)的研究与实践之Telegraf
- 【自然框架】之通用权限(九):权限的验证
- 【自然框架】之通用权限(八):权限到字段(列表、表单、查询)
- 【自然框架】之通用权限(七):权限到按钮
- 通过预测API窃取机器学习模型
- 【自然框架】 页面里的父类——把共用的东东都交给父类,让子类专注于其他。
- 血淋林的例子告诉你,为什么防“上传漏洞”要用白名单
- 关于Int自增字段和GUID字段的性能测试。只有测试,没有分析,呵呵
- 【自然框架】 之 资源角色——列表过滤方案(思路篇)
- UVM(七)之phase及objection
- 【自然框架】 之 主从表的添加、修改
- HLS Lesson6-数据类型转换
- 某开源框架从注入到Getshell
- HLS Lesson4-例子
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC
- 【tensorflow2.0】AutoGraph的使用规范
- django-个人网站之环境配置(一)
- 用C++跟你聊聊“职责链模式”
- django-常见问题勘误
- 【tensorflow2.0】AutoGraph和tf.Module
- mybatis之第一个mybatis程序(二)
- mybatis-spring整合的三种(逐渐优化)方案
- 用C++跟你聊聊“命令模式”,跟我聊就够了
- 【tensorflow2.0】数据管道dataset
- 用C++跟你聊聊“桥接模式”
- 用C++跟你聊聊“单例模式”,类的“计划生育”
- django-HttpResponse,render,redirect
- django-ForeignKey,OneToOneField,ManyToManyField
- 用C++跟你聊聊“备忘录模式” ,如果能重来,我要···
- 数据库(一)--通过django创建数据库表并填充数据