文件目录结构及文件MD5的PHP获取方法

废话不多说 直接上代码

第一种形式——直接输出树结构

function tree($AppPath){
    //忽略的目录
    $ignoreDir = array('***');

    echo "\n";
    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
             if(in_array($subDir, $ignoreDir)){
                      continue;
              }
              echo " $file\n";
              tree ( $subDir);
        } else {
          // Sub is a file.
          echo "{$subDir} ".md5_file($subDir)."\n";
          //tree( $subDir );
        }
    }

    // close dir
    echo "\n";
    closedir ( $dh );
}

$dir = './我要展示的目录';
tree($dir);

第二种形式——以二维数组输出到PHP文件

class TestGenerate {
        public static $appFolder = "";
        public static $ignoreArr = array (
              "myPath",
              "如:C:/Users/Administrator/Desktop/",
          );

       public static function start() {
                $AppPath = "C:/Users/Administrator/Desktop/gc/GC_bak_0926";
                TestGenerate::$appFolder = $AppPath;
                $destManifestPath = "./file.md5.php";

                // 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 );
        }

        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 . '/' . $file;
                if(in_array($subDir, self::$ignoreArr)){
                          continue;
                }
                if ($file == "." || $file == "..") {
                          continue;
                  } else if (is_dir ( $subDir )) {
                        // rescure
                      TestGenerate::traverse ( $subDir, $manifestHandle );
                  } else {
                        // Sub is a file.
                          echo $subDir.'';
                          TestGenerate::writeOneFieToManifest ( $subDir, $manifestHandle );
                  }
        }

        // close dir
        closedir ( $dh );
      }

        public static function writeOneFieToManifest($filePath, $fileHandle) {
                  if (! file_exists ( $filePath )) {
                          continue;
                  }

                  $relativePath = str_replace ( TestGenerate::$appFolder .'/', '', $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 .= ' => "';
                $content .= $fileMd5;
                $content .= '",';
                $content .= "\n";

                if (! fwrite ( $fileHandle, $content )) {
                          print ($filePath . " can not be written!") ;
                }
        }

        public static function writeMaifestHeader($fileHandle) {
                $header = " $header .= "\n";
                $header .= "// This file is automatically generated";
                $header .= "\n";
                $header .= "\n";
                $header .= "\n";
                $header .= "\tstatic \$allFiles=array(";
                $header .= "\n";

                if (! fwrite ( $fileHandle, $header )) {
                        printf ( "Failure while write file header." );
                }
        }

        public static function writeMaifestFooter($fileHandle) {
                $footer = "\t);";
                $footer .= "\n";

                if (! fwrite ( $fileHandle, $footer )) {
                          printf ( "Failure while write file header." );
                 }
        }
}

// Start application
TestGenerate::start ();

你可能感兴趣的:(文件目录结构及文件MD5的PHP获取方法)