您正在使用 IPV4 [3.15.225.173] 访问本站,您本次已经查看了 1 页
用户名: 密 码: 验证码:     用QQ登录本站
首页 软件 编程 笑话 知识 公告 台风 日历 计算器
悟空收录网       [公益]保护绿色环境,构建和谐社会      

【腾讯云】2核2G4M云服务器新老同享99元/年,续费同价      
[公益] 地球是我家,绿化靠大家      
2024年 劳动节 001
2024年 端午节 041
2025年 元 旦 246
2025年 春 节 274
 
您现在的位置:首页 >> PHP >> 内容
本类新增
本类热门
php 遍历目录,生成目录下每个文件的md5值并写入到结果文件中
内容摘要: ?php/***@authorAdministrator**/classTestGenerate{publicstatic$appFolder='';publicstatic$ignoreFilePaths=array('xxxx/xxx.php');publicstaticfunctionstart(){$AppPath='E:\\myApp';TestG......
<?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();

?>

版权声明:本内容来源于网络,如有侵犯您的版权,请联系站长,本站收到您的信息后将及时处理。
上一篇:php实现遍历文件夹的方法汇总

 

下一篇:php 根据url自动生成缩略图并处理高并发问题

发布日期:2024/2/1
手机扫二维码直达本页
发布时间:11:04:07
点  击:20
录  入:星儿
相关文章
Baidu
YiJiaCMS 7.3.8 build231228(MSSQL) 闽ICP备05000814号-1
本空间由腾讯云(轻量应用服务器)提供,奇安信网站卫士提供加速防护
运行时间载入中.....