php 遍历目录,生成目录下每个文件的md5值并写入到结果文件中
php遍历目录,生成目录下每个文件的md5值并写入到结果文件中
实例代码:
<?php /** *@authorAdministrator * */ classTestGenerate{ publicstatic$appFolder=""; publicstatic$ignoreFilePaths=array( "xxxx/xxx.php" ); publicstaticfunctionstart(){ $AppPath="E:\\myApp"; TestGenerate::$appFolder=$AppPath; $destManifestPath="E:\\temp2\\dest.md5.txt"; //destfilehandle $manifestHandle=fopen($destManifestPath,"w+"); //writeheader TestGenerate::writeMaifestHeader($manifestHandle); //writemd5 TestGenerate::traverse($AppPath,$manifestHandle); //writefooter TestGenerate::writeMaifestFooter($manifestHandle); //closefile fclose($manifestHandle); } /** *遍历应用根目录下的文件,并生成对应的文件长度及md5信息 * *@paramunknown$AppPath *应用根目录,如:xxx/xxx/analytics *@paramstring$destManifestPath *生成的manifest文件存放位置的文件句柄 */ publicstaticfunctiontraverse($AppPath,$manifestHandle){ if(!file_exists($AppPath)){ printf($AppPath."doesnotexist!"); return; } if(!is_dir($AppPath)){ printf($AppPath."isnotadirectory!"); return; } if(!($dh=opendir($AppPath))){ printf("Failurewhilereaddiectory!"); return; } //readfiles while(($file=readdir($dh))!=false){ $subDir=$AppPath.DIRECTORY_SEPARATOR.$file; if($file=="."||$file==".."){ continue; }elseif(is_dir($subDir)){ //rescure TestGenerate::traverse($subDir,$manifestHandle); }else{ //Subisafile. TestGenerate::writeOneFieToManifest($subDir,$manifestHandle); } } //closedir closedir($dh); } /** *写一个文件的md5信息到文件中 * *@paramunknown$filePath *@paramunknown$fileHandle */ publicstaticfunctionwriteOneFieToManifest($filePath,$fileHandle){ if(!file_exists($filePath)){ continue; } $relativePath=str_replace(TestGenerate::$appFolder.DIRECTORY_SEPARATOR,'',$filePath); $relativePath=str_replace("\\","/",$relativePath); //ignoretmpdirectory 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."cannotbewritten!"); } } /** *在manifes文件中写入头信息 * *@paramunknown$fileHandle */ publicstaticfunctionwriteMaifestHeader($fileHandle){ $header="<?php"; $header.="\n"; $header.="//Thisfileisautomaticallygenerated"; $header.="\n"; $header.="namespacetest;"; $header.="\n"; $header.="classMyFile{"; $header.="\n"; $header.="\tstatic\$allFiles=array("; $header.="\n"; if(!fwrite($fileHandle,$header)){ printf("Failurewhilewritefileheader."); } } /** *在manifes文件中写入尾部信息 * *@paramunknown$fileHandle */ publicstaticfunctionwriteMaifestFooter($fileHandle){ $footer="\t);"; $footer.="\n"; $footer.="}"; $footer.="\n"; if(!fwrite($fileHandle,$footer)){ printf("Failurewhilewritefileheader."); } } } //Startapplication TestGenerate::start(); ?>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!