欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-04-01 20:00:28
php 遍历目录,生成目录下每个文件的md5值并写入到结果文件中 实例代码:

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 application
testgenerate::start ();
 
?>

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!