首頁 後端開發 php教程 php遍歷目錄,產生目錄下每個檔案的md5值並寫入到結果檔案中

php遍歷目錄,產生目錄下每個檔案的md5值並寫入到結果檔案中

Jul 23, 2016 am 08:55 AM

  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 = "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 . " 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


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

在PHP API中說明JSON Web令牌(JWT)及其用例。 在PHP API中說明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

會話如何劫持工作,如何在PHP中減輕它? 會話如何劫持工作,如何在PHP中減輕它? Apr 06, 2025 am 12:02 AM

會話劫持可以通過以下步驟實現:1.獲取會話ID,2.使用會話ID,3.保持會話活躍。在PHP中防範會話劫持的方法包括:1.使用session_regenerate_id()函數重新生成會話ID,2.通過數據庫存儲會話數據,3.確保所有會話數據通過HTTPS傳輸。

PHP 8.1中的枚舉(枚舉)是什麼? PHP 8.1中的枚舉(枚舉)是什麼? Apr 03, 2025 am 12:05 AM

PHP8.1中的枚舉功能通過定義命名常量增強了代碼的清晰度和類型安全性。 1)枚舉可以是整數、字符串或對象,提高了代碼可讀性和類型安全性。 2)枚舉基於類,支持面向對象特性,如遍歷和反射。 3)枚舉可用於比較和賦值,確保類型安全。 4)枚舉支持添加方法,實現複雜邏輯。 5)嚴格類型檢查和錯誤處理可避免常見錯誤。 6)枚舉減少魔法值,提升可維護性,但需注意性能優化。

描述紮實的原則及其如何應用於PHP的開發。 描述紮實的原則及其如何應用於PHP的開發。 Apr 03, 2025 am 12:04 AM

SOLID原則在PHP開發中的應用包括:1.單一職責原則(SRP):每個類只負責一個功能。 2.開閉原則(OCP):通過擴展而非修改實現變化。 3.里氏替換原則(LSP):子類可替換基類而不影響程序正確性。 4.接口隔離原則(ISP):使用細粒度接口避免依賴不使用的方法。 5.依賴倒置原則(DIP):高低層次模塊都依賴於抽象,通過依賴注入實現。

在PHPStorm中如何進行CLI模式的調試? 在PHPStorm中如何進行CLI模式的調試? Apr 01, 2025 pm 02:57 PM

在PHPStorm中如何進行CLI模式的調試?在使用PHPStorm進行開發時,有時我們需要在命令行界面(CLI)模式下調試PHP�...

如何用PHP的cURL庫發送包含JSON數據的POST請求? 如何用PHP的cURL庫發送包含JSON數據的POST請求? Apr 01, 2025 pm 03:12 PM

使用PHP的cURL庫發送JSON數據在PHP開發中,經常需要與外部API進行交互,其中一種常見的方式是使用cURL庫發送POST�...

解釋PHP中的晚期靜態綁定(靜態::)。 解釋PHP中的晚期靜態綁定(靜態::)。 Apr 03, 2025 am 12:04 AM

靜態綁定(static::)在PHP中實現晚期靜態綁定(LSB),允許在靜態上下文中引用調用類而非定義類。 1)解析過程在運行時進行,2)在繼承關係中向上查找調用類,3)可能帶來性能開銷。

See all articles