php 遍歷目錄,生成目錄下每個文件的md5值并寫入到結(jié)果文件中
更新時間:2016年12月12日 08:48:15 投稿:lqh
本文章向大家介紹php遍歷目錄,生成目錄下每個文件的md5值并寫入到結(jié)果文件中,需要的朋友可以參考下
php 遍歷目錄,生成目錄下每個文件的md5值并寫入到結(jié)果文件中
實例代碼:
<?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實現(xiàn)遍歷文件夾的方法匯總
- PHP遍歷目錄文件的常用方法小結(jié)
- PHP遞歸遍歷指定文件夾內(nèi)的文件實現(xiàn)方法
- 遍歷指定目錄,并存儲目錄內(nèi)所有文件屬性信息的php代碼
- PHP目錄與文件操作技巧總結(jié)(創(chuàng)建,刪除,遍歷,讀寫,修改等)
- PHP遞歸遍歷指定目錄的文件并統(tǒng)計文件數(shù)量的方法
- PHP動態(tài)地創(chuàng)建屬性和方法, 對象的復(fù)制, 對象的比較,加載指定的文件,自動加載類文件,命名空間
- PHP實現(xiàn)的文件瀏覽器功能簡單示例
相關(guān)文章
php使用FFmpeg接口獲取視頻的播放時長、碼率、縮略圖以及創(chuàng)建時間
本篇文章主要介紹了php使用FFmpeg接口獲取視頻的播放時長、碼率、縮略圖以及創(chuàng)建時間,具有一定的參考價值,有需要的可以了解一下。2016-11-11使用php批量刪除數(shù)據(jù)庫下所有前綴為prefix_的表
這篇文章主要介紹了php如何批量刪除數(shù)據(jù)庫下所有前綴為prefix_的表,需要的朋友可以參考下2014-06-06PHP封裝curl的調(diào)用接口及常用函數(shù)詳解
今天小編就為大家分享一篇PHP封裝curl的調(diào)用接口及常用函數(shù)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05php提取身份證號碼中的生日日期以及驗證是否為成年人的函數(shù)
本篇文章使用php技術(shù)提取身份證號碼中的生日日期來判斷是否是未成年人的一個函數(shù)。下面小編把代碼分享給大家,供大家參考2015-09-09