PHP traverses the directory, generates the MD5 value of each file in the directory and writes to the result file _php instance

Source: Internet
Author: User
Tags md5

PHP traverses the directory, generates the MD5 value of each file in the directory and writes to the result file

Instance code:

<?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); /** * Traverses the file under the root directory and generates the corresponding file length and MD5 information * * @param unknown $AppPath * Application root directory, such as: Xxx/xxx/analytics * @p Aram String $destManifestPath * The file handle of the manifest file location that is generated */public static function traverse ($AppPath, $manifestH Andle) {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); /** * Write a file MD5 information to the file * * @param unknown $filePath * @param unknown $fileHandle/Pub Lic 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);
     
    $fileMd 5 = @md5_file ($filePath);
    $content = "\t\t";
    $content. = ' "';
    $content. = $relativePath;
    $content. = ' "';
    $content. = ' => array (';
    $content. = $fileSize;
    $content. = ' "," ';
    $content. = $fileMd 5;
    $content. = ' "), ';
     
    $content. = "\ n"; if (! fwrite ($fileHandle, $content)) {print ($filePath. "Can not be written!")
    ; /** * Write header information in manifes file * * @param unknown $fileHandle/public static function Writemaife
    Stheader ($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."); }/** * Write tail information in manifes file * * @param unknown $fileHandle/public static function Writemaif
    Estfooter ($fileHandle) {$footer = "t);";
    $footer. = "\ n";
    $footer. = "}";
     
    $footer. = "\ n";
    if (! fwrite ($fileHandle, $footer)) {printf ("failure while write file header.");
 
}}//Start application Testgenerate::start (); ?>

Thank you for reading, I hope to help you, thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.