php 遍歷目錄,生成目錄下每個文件的md5值并寫入到結(jié)果文件中
更新時間:2016年12月12日 08:48:15 投稿:lqh
本文章向大家介紹php遍歷目錄,生成目錄下每個文件的md5值并寫入到結(jié)果文件中,需要的朋友可以參考下
php 遍歷目錄,生成目錄下每個文件的md5值并寫入到結(jié)果文件中
實(shí)例代碼:
<?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 );
}
/**
* 遍歷應(yīng)用根目錄下的文件,并生成對應(yīng)的文件長度及md5信息
*
* @param unknown $AppPath
* 應(yīng)用根目錄,如:xxx/xxx/analytics
* @param string $destManifestPath
* 生成的manifest文件存放位置的文件句柄
*/
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 . 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 );
}
/**
* 寫一個文件的md5信息到文件中
*
* @param unknown $filePath
* @param unknown $fileHandle
*/
public 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 );
$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 . " can not be written!") ;
}
}
/**
* 在manifes文件中寫入頭信息
*
* @param unknown $fileHandle
*/
public static function writeMaifestHeader($fileHandle) {
$header = "<?php";
$header .= "\n";
$header .= "http:// 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." );
}
}
/**
* 在manifes文件中寫入尾部信息
*
* @param unknown $fileHandle
*/
public static function writeMaifestFooter($fileHandle) {
$footer = "\t);";
$footer .= "\n";
$footer .= "}";
$footer .= "\n";
if (! fwrite ( $fileHandle, $footer )) {
printf ( "Failure while write file header." );
}
}
}
// Start application
TestGenerate::start ();
?>
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:
- php遍歷目錄下文件并按修改時間排序操作示例
- PHP超低內(nèi)存遍歷目錄文件和讀取超大文件的方法
- php實(shí)現(xiàn)遍歷文件夾的方法匯總
- PHP遍歷目錄文件的常用方法小結(jié)
- PHP遞歸遍歷指定文件夾內(nèi)的文件實(shí)現(xiàn)方法
- 遍歷指定目錄,并存儲目錄內(nèi)所有文件屬性信息的php代碼
- PHP目錄與文件操作技巧總結(jié)(創(chuàng)建,刪除,遍歷,讀寫,修改等)
- PHP遞歸遍歷指定目錄的文件并統(tǒng)計文件數(shù)量的方法
- PHP動態(tài)地創(chuàng)建屬性和方法, 對象的復(fù)制, 對象的比較,加載指定的文件,自動加載類文件,命名空間
- PHP實(shí)現(xiàn)的文件瀏覽器功能簡單示例
相關(guān)文章
Codeigniter的一些優(yōu)秀特性總結(jié)
本文總結(jié)了一下Codeigniter的一些優(yōu)秀特性,算是個人的一些心得分享,推薦給大家。2015-01-01
thinkPHP框架對接支付寶即時到賬接口回調(diào)操作示例
這篇文章主要介紹了thinkPHP框架對接支付寶即時到賬接口回調(diào)操作,結(jié)合實(shí)例形式分析了thinkPHP針對支付寶接口回調(diào)操作的原理與具體操作步驟,需要的朋友可以參考下2016-11-11
Laravel框架基于ajax實(shí)現(xiàn)二級聯(lián)動功能示例
這篇文章主要介紹了Laravel框架基于ajax實(shí)現(xiàn)二級聯(lián)動功能,結(jié)合實(shí)例形式分析了Laravel框架ajax數(shù)據(jù)讀取及聯(lián)動菜單的構(gòu)造與使用技巧,需要的朋友可以參考下2019-01-01
phpcms手機(jī)內(nèi)容頁面添加上一篇和下一篇
本文給大家分享的是在phpcms中的手機(jī)內(nèi)容詳情頁里添加上一篇和下一篇的方法,十分的簡單實(shí)用,有需要的小伙伴可以參考下。2015-06-06

