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!