php遍历目录,生成目录下每个文件的md5值并写入到结果文件中

前端之家收集整理的这篇文章主要介绍了php遍历目录,生成目录下每个文件的md5值并写入到结果文件中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

  1. <?PHP
  2.  
  3. /**
  4. * @author Administrator
  5. *
  6. */
  7. class TestGenerate {
  8. public static $appFolder = "";
  9. public static $ignoreFilePaths = array (
  10. "xxxx/xxx.PHP"
  11. );
  12. public static function start() {
  13. $AppPath = "E:\\myApp";
  14. TestGenerate::$appFolder = $AppPath;
  15. $destManifestPath = "E:\\temp2\\dest.md5.txt";
  16. // dest file handle
  17. $manifestHandle = fopen ( $destManifestPath,"w+" );
  18. // write header
  19. TestGenerate::writeMaifestHeader ( $manifestHandle );
  20. // write md5
  21. TestGenerate::traverse ( $AppPath,$manifestHandle );
  22. // write footer
  23. TestGenerate::writeMaifestFooter ( $manifestHandle );
  24. // close file
  25. fclose ( $manifestHandle );
  26. }
  27. /**
  28. * 遍历应用根目录下的文件,并生成对应的文件长度及md5信息
  29. *
  30. * @param unknown $AppPath
  31. * 应用根目录,如:xxx/xxx/analytics
  32. * @param string $destManifestPath
  33. * 生成的manifest文件存放位置的文件句柄
  34. */
  35. public static function traverse($AppPath,$manifestHandle) {
  36. if (! file_exists ( $AppPath )) {
  37. printf ( $AppPath . " does not exist!" );
  38. return;
  39. }
  40. if (! is_dir ( $AppPath )) {
  41. printf ( $AppPath . " is not a directory!" );
  42. return;
  43. }
  44. if (! ($dh = opendir ( $AppPath ))) {
  45. printf ( "Failure while read diectory!" );
  46. return;
  47. }
  48. // read files
  49. while ( ($file = readdir ( $dh )) != false ) {
  50. $subDir = $AppPath . DIRECTORY_SEPARATOR . $file;
  51. if ($file == "." || $file == "..") {
  52. continue;
  53. } else if (is_dir ( $subDir )) {
  54. // rescure
  55. TestGenerate::traverse ( $subDir,$manifestHandle );
  56. } else {
  57. // Sub is a file.
  58. TestGenerate::writeOneFieToManifest ( $subDir,$manifestHandle );
  59. }
  60. }
  61. // close dir
  62. closedir ( $dh );
  63. }
  64. /**
  65. * 写一个文件的md5信息到文件
  66. *
  67. * @param unknown $filePath
  68. * @param unknown $fileHandle
  69. */
  70. public static function writeOneFieToManifest($filePath,$fileHandle) {
  71. if (! file_exists ( $filePath )) {
  72. continue;
  73. }
  74. $relativePath = str_replace ( TestGenerate::$appFolder . DIRECTORY_SEPARATOR,'',$filePath );
  75. $relativePath = str_replace ( "\\","/",$relativePath );
  76. // ignore tmp directory
  77. if (strpos ( $relativePath,"tmp/" ) === 0) {
  78. return;
  79. }
  80. $fileSize = filesize ( $filePath );
  81. $fileMd5 = @md5_file ( $filePath );
  82. $content = "\t\t";
  83. $content .= '"';
  84. $content .= $relativePath;
  85. $content .= '"';
  86. $content .= ' => array("';
  87. $content .= $fileSize;
  88. $content .= '","';
  89. $content .= $fileMd5;
  90. $content .= '"),';
  91. $content .= "\n";
  92. if (! fwrite ( $fileHandle,$content )) {
  93. print ($filePath . " can not be written!") ;
  94. }
  95. }
  96. /**
  97. * 在manifes文件中写入头信息
  98. *
  99. * @param unknown $fileHandle
  100. */
  101. public static function writeMaifestHeader($fileHandle) {
  102. $header = "<?PHP";
  103. $header .= "\n";
  104. $header .= "// This file is automatically generated";
  105. $header .= "\n";
  106. $header .= "namespace test;";
  107. $header .= "\n";
  108. $header .= "class MyFile {";
  109. $header .= "\n";
  110. $header .= "\tstatic \$allFiles=array(";
  111. $header .= "\n";
  112. if (! fwrite ( $fileHandle,$header )) {
  113. printf ( "Failure while write file header." );
  114. }
  115. }
  116. /**
  117. * 在manifes文件中写入尾部信息
  118. *
  119. * @param unknown $fileHandle
  120. */
  121. public static function writeMaifestFooter($fileHandle) {
  122. $footer = "\t);";
  123. $footer .= "\n";
  124. $footer .= "}";
  125. $footer .= "\n";
  126. if (! fwrite ( $fileHandle,$footer )) {
  127. printf ( "Failure while write file header." );
  128. }
  129. }
  130. }
  131.  
  132. // Start application
  133. TestGenerate::start ();
  134.  
  135. ?>


以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

猜你在找的PHP相关文章