Home > Backend Development > PHP Tutorial > PHP code reference for searching files and listing file names

PHP code reference for searching files and listing file names

WBOY
Release: 2016-07-25 08:58:00
Original
771 people have browsed it
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);
    }
}
?>
Copy after login

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","/&#039;/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>";
}
}
?>
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template