PHP搜索文件且列出文件名的代码参考

php中文网
发布: 2016-07-25 08:58:00
原创
1007人浏览过
本文介绍下,php搜索文件并列出搜索到的文件名的一例代码,供初学的朋友参考。

先来看一个简单的php搜索文件并显示的代码。

如下:

<?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);
    }
}
?>
登录后复制

下面是功能更强,复杂点的。

jQuery+PHP实现微信大转盘抽奖
jQuery+PHP实现微信大转盘抽奖

微信大转盘抽奖-jQuery+PHP实现,发现很多转盘抽奖,都实现了前台部分,大部分都使用了HTML5技术,但是后台自己调整抽奖几率不方便,索性自己收集资料,在一个转盘抽奖的基础上,增加了PHP部分代码,可以对抽奖几率进行自定义,设置请参考data.php文件,相关部分给出了注释。

jQuery+PHP实现微信大转盘抽奖 0
查看详情 jQuery+PHP实现微信大转盘抽奖
<?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搜索文件且列出文件名的代码参考 " >)+/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>";
}
}
?>
登录后复制


PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号