php 遍曆目錄,組建目錄下每個檔案的md5值並寫入到結果檔案中

來源:互聯網
上載者:User
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 applicationTestGenerate::start ();  ?>
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.