Home >Backend Development >PHP Tutorial >PHP code reference for searching files and listing file names
This article introduces an example code that searches for files in PHP and lists the searched file names for the reference of beginners.
Let’s first look at a simple PHP code to search files and display them. As follows: <?php /** * 搜索文件并列出文件名 * opendir打开目录 * readdir读取文件名 * unlink删除文件 * edit by bbs.it-home.org */ $dir = "file/class/"; //获取目录下的文件 if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if($file!="."&&$file!=".."){ $unexit[]=$file;} } closedir($dh); } } ?> The following one is more powerful and more complex. <?php /** * 文件: search.php * 功能: 搜索指定目录下的HTML文件 */ /* 基本函数 */ //获取目录下文件函数 function getFile($dir) { $dp = opendir($dir); $fileArr = array(); while (!false == $curFile = readdir($dp)) { if ($curFile!="." && $curFile!=".." && $curFile!="") { if (is_dir($curFile)) { $fileArr = getFile($dir."/".$curFile); } else { $fileArr[] = $dir."/".$curFile; } } } return $fileArr; } //获取文件内容 function getFileContent($file) { if (!$fp = fopen($file, "r")) { die("Cannot open file $file"); } while ($text = fread($fp, 4096)) { $fileContent .= $text; } return $fileContent; } //搜索指定文件 function searchText($file, $keyword) { $text = getFileContent($file); if (preg_match("/$keyword/i", $text)) { return true; } return false; } //搜索出文章的标题 function getFileTitle($file, $default="None subject") { $fileContent = getFileContent($file); $sResult = preg_match("/<title>.*<\/title>/i", $fileContent, $matchResult); $title = preg_replace(array("/(<title>)/i","/(<\/title>)/i"), "", $matchResult[0]); if (empty($title)) { return $default; } else { return $title; } } //获取文件描述信息 function getFileDescribe($file,$length=200, $default="None describe") { $metas = get_meta_tags($file); if ($meta['description'] != "") { return $metas['description']; } $fileContent = getFileContent($file); preg_match("/(<body.*<\/body>)/is", $fileContent, $matchResult); $pattern = array("/(<[^\x80-\xff]+>)/i","/(<input.*>)+/i", "/()+/i", "/(<img .* alt="PHP code reference for searching files and listing file names" >)+/i", "/([<script.*>])+.*([<\/script>])+/i","/&/i","/"/i","/'/i", "/\s/"); $description = preg_replace($pattern, "", $matchResult[0]); $description = mb_substr($description, 0, $length)." ..."; return $description; } //加亮搜索结果中的关键字 function highLightKeyword($text, $keyword, $color="#C60A00") { $newword = "<font color=$color>$keyword</font>"; $text = str_replace($keyword, $newword, $text); return $text; } //获取文件大小(KB) function getFileSize($file) { $filesize = intval(filesize($file)/1024)."K"; return $filesize; } //获取文件最后修改的时间 function getFileTime($file) { $filetime = date("Y-m-d", filemtime($file)); return $filetime; } //搜索目录下所有文件 function searchFile($dir, $keyword) { $sFile = getFile($dir); if (count($sFile) <= 0) { return false; } $sResult = array(); foreach ($sFile as $file) { if (searchText($file, $keyword)) { $sResult[] = $file; } } if (count($sResult) <= 0) { return false; } else { return $sResult; } } //by bbs.it-home.org /* 测试代码 */ //指定要搜索的目录 $dir = "./php_Linux"; //要搜索的关键字 $keyword = "sendmail"; $fileArr = searchFile($dir, $keyword); $searchSum = count($fileArr); echo "搜索关键字: <b>$keyword</b> 搜索目录: <b>$dir</b> 搜索结果: <b>$searchSum</b><br><hr size=1><br>"; if ($searchSum <= 0) { echo "没有搜索到任何结果"; } else { foreach ($fileArr as $file) { echo "". highLightKeyword(getFileTitle($file), $keyword) . " - ".getFileSize($file)." ". getFileTime($file) . "<br>\n<font size=2>".highLightKeyword(getFileDescribe($file), $keyword) . "</font><br><br>"; } } ?> |