ホームページ  >  記事  >  バックエンド開発  >  PHP はディレクトリを走査し、ディレクトリ内の各ファイルの md5 値を生成し、それを結果ファイルに書き込みます。

PHP はディレクトリを走査し、ディレクトリ内の各ファイルの md5 値を生成し、それを結果ファイルに書き込みます。

WBOY
WBOYオリジナル
2016-07-23 08:55:00942ブラウズ
  1. /**
  2. * @著者管理者
  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 ファイルハンドル
  15. $manifestHandle = fopen ( $destManifestPath, "w+" );
  16. // ヘッダーを書き込みます
  17. TestGenerate::writeMaifestHeader ( $manifestHandle );
  18. // md5 を書き込みます
  19. TestGenerate::traverse ( $AppPath, $manifestHandle );
  20. // フッターを書きます
  21. TestGenerate::writeMaifestFooter ( $manifestHandle );
  22. // ファイルを閉じる
  23. fclose ( $manifestHandle );
  24. }
  25. /**
  26. * アプリケーションのルート ディレクトリ内のファイルを走査し、対応するファイルの長さと md5 情報を生成します
  27. *
  28. * @param known $AppPath
  29. * アプリケーションのルート ディレクトリ (例: xxx/xxx/analytics
  30. * @param string $destManifestPath)
  31. * 生成されたマニフェストファイルの保存場所のファイルハンドル
  32. */
  33. public static function traverse($ AppPath, $manifestHandle) {
  34. if (! file_exists ( $AppPath )) {
  35. printf ( $AppPath . " が存在しません!" );
  36. return;
  37. }
  38. if (! is_dir ( $AppPath )) {
  39. printf ( $AppPath . " はディレクトリではありません!" );
  40. return;
  41. }
  42. if (! ($dh = opendir ( $AppPath ))) {
  43. printf ( "ディレクトリの読み取り中に失敗しました!" );
  44. return;
  45. }
  46. // ファイルを読み取ります
  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 はファイルです。
  56. TestGenerate::writeOneFieToManifest ( $subDir, $manifestHandle );
  57. }
  58. }
  59. // dir
  60. Closedir を閉じます ( $dh );
  61. }
  62. /**
  63. * ファイルのmd5情報をファイルに書き込みます
  64. *
  65. * @paramknown $filePath
  66. * @paramunknown $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. // tmp ディレクトリを無視します
  75. if (strpos ( $relativePath, "tmp/" ) === 0) {
  76. return;
  77. }
  78. $fileSize = filesize ( $filePath );
  79. $fileMd5 = @md5_file ( $filePath );
  80. $content = "tt";
  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 . " を書き込めません!") ;
  92. }
  93. }
  94. /**
  95. * ヘッダー情報をマニフェストファイルに書き込む
  96. *
  97. * @param known $fileHandle
  98. */
  99. public static function writeMaifestHeader($fileHandle) {
  100. $header = "
  101. $header .= "n";
  102. $header .= "// このファイルは自動的に生成されます";
  103. $header .= "n";
  104. $header .= "名前空間テスト;";
  105. $header .= "n";
  106. $header .= "class MyFile {";
  107. $header .= "n";
  108. $header .= "tstatic $allFiles= array(";
  109. $header .= "n";
  110. if (! fwrite ( $fileHandle, $header )) {
  111. printf ( "ファイル ヘッダーの書き込み中に失敗しました。" );
  112. }
  113. }
  114. /**
  115. * マニフェストファイルに末尾情報を書き込む
  116. *
  117. * @param known $fileHandle
  118. */
  119. public static function writeMaifestFooter($fileHandle) {
  120. $footer = "t);";
  121. $footer .= "n";
  122. $footer .= "}";
  123. $footer .= "n ";
  124. if (! fwrite ( $fileHandle, $footer )) {
  125. printf ( "ファイル ヘッダーの書き込み中に失敗しました。" );
  126. }
  127. }
  128. }
  129. // アプリケーションを開始します
  130. TestGenerate::start ();
  131. ?>
复制代


目录下、php


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。