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

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


目录下, php


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!