很久以前就写了,现在拿到博客给大家分享一下。仿百度谷歌搜索下拉自动提示原理并不是很复杂,主要就是通过ajax这座桥梁。没有百度那么强大,它可以匹配拼音等,我目前水平确实做不到,我只是实现一下这个效果。
我们一起看看源码后面有分析与源码下载
数据库,我们把它保存导入到mysql数据库
代码如下 | 复制代码 |
/* Source Server : localhost Target Server Type : MYSQL Date: 2013-07-23 17:52:48
-- ---------------------------- |
index.html
代码如下 | 复制代码 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
getdata.php文件
代码如下 | 复制代码 |
<?php $link = mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd); ?> |
这些是核心代码,后面有完整实例下载地址
先看一下效果吧(往下面走,有源码下载^_^)
输入一个“p”后的效果
每输入一个字符都会进行一次匹配
效果就这样,如果觉得还行,可以下载下面的源码来玩玩。
数据表里面我只添加了10来条数据,如果有需要,可以自己添加。
整实例下载地址:源码下载
自动保存常用于编辑器了,要实现数据定时保存我们会使用到ajax功能,下面我们再一起来看一个input输入框失去焦点自动保存输入框的数据实例最近做一个输入框失去焦点时自动保存数据的功能,当然就是jQuery选择器选择input,blur时,ajax提交数据给php文件,php文件保存一下数据咯。主要是要注意一下中文的问题,所以中间需要转一下编码。
下面的实例是一个列表页,有一点类似excel了。
html代码:
代码如下 | 复制代码 |
<table width=”100%” align=”left” border=”0″ cellspacing=”0″ bgcolor=”#CCCCCC”> |
js代码:
代码如下 | 复制代码 |
<script type=”text/javascript”> function Dd(i) {return document.getElementById(i);} |
php代码:(引用了dedecms的函数、方法)
代码如下 | 复制代码 |
if($dopost==’ajaxSave’) $value = urldecode(AutoCharset($value,”UTF-8″,”GB2312″)); } |
方法二,定时保存草稿功能,防止数据意外丢失!
情况介绍,网站后台编辑器是采用了百度UEditor所见即所得编辑器,我们就是要实现自动定时保存编辑器里的内容。
编辑器的调用方式如下:
代码如下 | 复制代码 |
<script type="text/plain" id="content" name="content" style="width:800px;height:400px;"></script> |
在发表页面或编辑页的最后加入js调用标签:
<!--自动保存功能 防止数据意外丢失-->
代码如下 | 复制代码 |
<script type="text/javascript"" width=100% src="localStorag.js"></script> |
localStorag.js 的代码如下:
代码如下 | 复制代码 |
/*注意: 本js脚本请在网页源代码最后的地方放置*/ var spanObj = document.getElementById('s1'); function stoplocs(){ function showlocs(){ } |
可以增加停止保存按钮或立即恢复按钮,如下:
代码如下 | 复制代码 |
<a href="javascript:void(0)" onclick="javascript:stoplocs()">停止保存</a> |
好了,现在你的网站发表页面/编辑页面就具备自动保存功能了。注意这种方式支持IE8及以上版本的IE,CHROME,Firefox等等大多数主流浏览器。至于IE6,实在是老掉牙了,不在考虑范围内。如果一定要考虑IE6的,请使用上面提供的IE6/ie7兼容方案。
今天我们一起来看看在php中获取目录及删除目录中文件或指定目录中文件实例,希望此方法对各位朋友会带来帮助。用php程序删除目录和文件比到ftp删除有很多优势。首先进ftp就比较麻烦,然后删除大量文件的时候ftp删除很慢,还有就是程序只想删除文件夹下面的文件,而保留目录结构,可以用php来轻松的实现,再有比如生成的日志、缓存要清除用程序来完成显然轻松的多。
第一步,PHP获取目录下面所有文件和文件夹
代码如下 | 复制代码 |
<?php $dir = "D:/"; //要获取的目录 echo "********** 获取目录下所有文件和文件夹 ***********<hr/>"; //先判断指定的路径是不是一个文件夹 if (is_dir($dir)){ if ($dh = opendir($dir)){ while (($file = readdir($dh))!= false){ //文件名的全路径 包含文件名 $filePath = $dir.$file; //获取文件修改时间 $fmt = filemtime($filePath); echo "<span style='color:#666'>(".date("Y-m-d H:i:s",$fmt).")</span> ".$filePath."<br/>"; } closedir($dh); } } ?> |
第2步:删除目录及目录下所有的文件
代码如下 | 复制代码 |
if ( $handle = opendir( "$dirName" ) ) { while ( false !== ( $item = readdir( $handle ) ) ) { if ( $item != "." && $item != ".." ) { if ( is_dir( "$dirName/$item" ) ) { delDirAndFile( "$dirName/$item" ); } else { if( unlink( "$dirName/$item" ) )echo "已删除文件: $dirName/$item<br />n"; } } } closedir( $handle ); if( rmdir( $dirName ) ) echo "成功删除目录: $dirName<br />n"; } }
|
第3步:删除目录下面的文件,不删除目录
代码如下 | 复制代码 |
if ( $handle = opendir( "$dirName" ) ) { while ( false !== ( $item = readdir( $handle ) ) ) { if ( $item != "." && $item != ".." ) { if ( is_dir( "$dirName/$item" ) ) { delFileUnderDir( "$dirName/$item" ); } else { if( unlink( "$dirName/$item" ) ) echo "已删除文件:$dirName/$item<br />n"; } } } closedir( $handle ); } } |
小结
这个我们先是从获取目录所有所文件与目录开始然后在基础上进行删除操作。
php mkdir创建目录只能一级级创建目录了,如果多级我们需要递归遍历来创建目录哦,下面我给大家介绍此函数的一些使用技巧吧。先介绍一下 mkdir() 这个函数
mkdir($path,0777,true);
第一个参数:必须,代表要创建的多级目录的路径;
第二个参数:设定目录的权限,默认是 0777,意味着最大可能的访问权;
第三个参数:true表示允许创建多级目录。
mkdir($dir,$mode);但是它每次只能创建一个目录,也就是说它不能一次创建多级目录,如下
mkdir('aa'); //就只能创建一个aa目录了
mkdir('aa/bb/cc');//如果有aa/bb目录就可以成功创建cc目录否则会报错哦,如果要创建多目录我们看下面代码
举例代码(支持创建中文目录):
代码如下 | 复制代码 |
<?php
//要创建的多级目录 $path="dai/php/php学习"; //判断目录存在否,存在给出提示,不存在则创建目录 if (is_dir($path)){ echo "对不起!目录 " . $path . " 已经存在!"; }else{ //第三个参数是“true”表示能创建多级目录,iconv防止中文目录乱码 $res=mkdir(iconv("UTF-8", "GBK", $path),0777,true); if ($res){ echo "目录 $path 创建成功"; }else{ echo "目录 $path 创建失败"; } }
|
再看一个递归创建目录的实例
小编整理了两个可以递归创建目录的方法供大家参考学习,谢谢啦!
代码如下 | 复制代码 |
<?php function mkdirs($dir, $mode = 0777) |
上边介绍的就是php递归创建目录和多级目录的内容
搜索文件很简单只要用户输入目录我们就会自动遍历目录找到相关联的文件名并列出来,下面我们一起来看看吧。php文件查找程序,输入一个路径确定后会遍历目录下所有的文件和文件夹,通过递归可以找到文件夹下面的每一个文件,再通过文件名和输入的关键字匹配,则可以查找到你想要的文件。对于本地,我们可以利用windows自带的查找去进行查找,但是对于线上的话,如查找ftp空间里面文件,本程序是很有用的。
php文件查找器源码:
代码如下 | 复制代码 |
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>php版文件查找(file search)</title> </head> <body> <form action="" method="post"> <p> 文件查找(注:区分大小写)</p> <p>路径:<input type="text" name="path" /></p> <p>查找:<input type="text" name="key" /></p> <p><input type="submit" name="sub" value=" 开 始 " /></p> </form> </body> </html> <?php /* * 注:区分大小写 */ if(!empty($_POST['path'])&&!empty($_POST['key'])){ echo "在路径 ".$_POST['path']."/ 中查找 ".$_POST['key']." 的结果为:<hr/>"; $file_num = $dir_num = 0; $r_file_num = $r_dir_num= 0; $findFile = $_POST['key']; function delDirAndFile( $dirName ){ if ( $handle = @opendir( "$dirName" ) ) { while ( false !== ( $item = readdir( $handle ) ) ) { if ( $item != "." && $item != ".." ) { if ( is_dir( "$dirName/$item" ) ) { delDirAndFile( "$dirName/$item" ); } else { $GLOBALS['file_num']++; if(strstr($item,$GLOBALS['findFile'])){ echo " <span><b> $dirName/$item </b></span><br />n"; $GLOBALS['r_file_num']++; } } } } closedir( $handle ); $GLOBALS['dir_num']++; if(strstr($dirName,$GLOBALS['findFile'])){ $loop = explode($GLOBALS['findFile'],$dirName); $countArr = count($loop)-1; if(empty($loop[$countArr])){ echo " <span style='color:#297C79;'><b> $dirName </b></span><br />n"; $GLOBALS['r_dir_num']++; } } }else{ die("没有此路径!"); } }
echo "<hr/>本次共搜索到".$file_num."个文件,文件夹".$dir_num."个<br/>"; echo "<hr/>符合结果的共".$r_file_num."个文件,文件夹".$r_dir_num."个<br/>"; }
|
上面只是查找文件,下面看一个查找文件中的字符是否包括我们要找的东西
自己写的一个批量查找文件内容的php程序,我是拿来扫描文件特征码的,现在我 贴出代码,供大家参考
代码如下 | 复制代码 |
if ($_POST ['Submit'] == '开始') { $total = 0; //文件总数 $dangerous = array (); //危险文件 $dangerous_content = $_POST ["sstr"]; $find_path = $_POST ["searchpath"]; $shortname = $_POST ["shortname"]; echo "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>"; echo "<html>"; echo "<head>"; echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />"; echo "</head>"; echo "<body>"; $begin_time=date("U"); // $dangerous_content = "小亮,Root_GP,Root_CSS,c99sh_updateurl,c99sh_sourcesurl,640684770"; visitFile ( $find_path, $shortname ); $end_time=date("U"); foreach ($dangerous as $d){ echo $d."<br/>"; } echo "查找文件总数:" . $total." 危险文件:".count($dangerous)." 总用时".($end_time-$begin_time)."秒"; echo "</body>"; echo "</html>"; //if (! empty ( $dangerous )) { //foreach ( $dangerous as $dan ) { //echo "[error]" . $dan . "<br/>"; //} //} exit(); } function visitFile($path, $ext) { global $total; global $dangerous_content; $fdir = dir ( $path ); //echo "Handle: " . $d->handle . "<br>"; // echo "Path: " . $fdir->path . "<br>"; set_time_limit ( 24 * 60 * 60 );
while ( ($entry = $fdir->read ()) !== false ) { $pathSub = $path . "\" . $entry; if ($entry != '.' && $entry != '..') { if (is_dir ( $pathSub )) { visitFile ( $pathSub, $ext ); } else { $exten = explode ( '.', $entry ); $exten = array_reverse ( $exten ); //把上面数组倒序 // foreach () $shortnames = explode ( '|', $ext ); foreach ( $shortnames as $sn ) { if (! empty ( $exten ) && $sn == $exten [0]) { $total = $total + 1; //echo "开始分析文件:".$path."/".$entry . "<br>"; $content = file_get_contents ( $path . "/" . $entry ); //这个性能较好 $content = strtolower ( $content ); //全部转为小写 $dangerous_content = strtolower ( $dangerous_content ); //全部转为小写 isExists ( $dangerous_content, $path . "/" . $entry, $content );//这个方法太耗内存了,希望有高手能解决一下 } } //sleep(1); } } } $fdir->close (); } function isExists($str, $filename, $content) { global $dangerous; //sleep ( 1 ); set_time_limit ( 10 ); $arr = explode ( ',', $str ); $signature="特征码:"; if (! empty ( $arr )) { // $content = file_get_contents ( $filename ); //这个性能较好 $content = strtolower ( $content ); //全部转为小写 $error_count = 0; foreach ( $arr as $a ) { if (trim ( $a ) != "") { if (strpos ( $content, $a )) { $error_count = $error_count + 1; $signature.=$a." "; } } } if ($error_count > 0) { // $dangerous [] = $filename; $dangerous [] = "[error] " . $error_count . " " .$signature." " . $filename; //echo "[error] " . $error_count . " " .$signature." " . $filename . "<br/>"; }else{ //echo "[ok] " . $filename . "<br/>"; } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>批量查询文件</title> <style type="text/css"> body { background: #FFFFFF; color: #000; font-size: 12px; }
#top { text-align: center; }
h1,p,form { margin: 0; padding: 0; }
h1 {font-size; 14px;
} </style> </head> <body> <div id="top"> <h1>批量查找程序</h1> <div>本程序可以扫描指定目录的所有文件,进行<strong>内容查找</strong>。<br /> 在文件数量非常多的情况下,本操作比较占用服务器资源,请确脚本超时限制时间允许更改,否则可能无法完成操作。</div> </div>
<form action="<?=$_SERVER ['SCRIPT_NAME']?>" name="form1" target="stafrm" method="post"> <table width="95%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#666666"> <tr> <td width="10%" bgcolor="#FFFFFF"><strong> 起始根路径:</strong></td> <td width="90%" bgcolor="#FFFFFF"><input name="searchpath" type="text" id="searchpath" value="D:/" size="20" /> 点表示当前目录,末尾不要加/ </td> </tr> <tr> <td bgcolor="#FFFFFF"><strong> 文件扩展名:</strong></td> <td bgcolor="#FFFFFF"><input name="shortname" type="text" id="shortname" size="20" value="htm|html|shtml|php" /> 多个请用|隔开</td> </tr> <tr id="rpct"> <td height="64" colspan="2" bgcolor="#FFFFFF"> <table width="100%" border="0" cellspacing="1" cellpadding="1"> <tr bgcolor="#EDFCE2"> <td colspan="4"><strong>内容查找选项:</strong> <input type="checkbox" name="isreg" value="1" />使用正则表达式</td> </tr> <tr> <td colspan="4">查找内容类默认使用字符串查找,也可以使用正则表达式(需勾选)。"查找为"不填写的话,就表示删除"查找内容"。 <br />com,system,exec,eval,escapeshell,cmd,passthru,base64_decode,gzuncompress </td> </tr> <tr> <td width="10%"> 查找内容:</td> <td width="36%" colspan="3"><textarea name="sstr" id="sstr" style="width: 90%; height: 45px">小亮,Root_GP,Root_CSS,c99sh_updateurl,c99sh_sourcesurl,640684770,hx_dealdir,while(1)</textarea></td> </tr> </table> </td> </tr> <tr> <td colspan="2" height="20" align="center" bgcolor="#E2F5BC"><input type="submit" name="Submit" value="开始" class="inputbut" /></td> </tr> </table> </form> <table width="95%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#666666"> <tr bgcolor="#FFFFFF"> <td id="mtd"> <div id='mdv' style='width: 100%; height: 100;'><iframe name="stafrm" frameborder="0" id="stafrm" width="100%" height="100%"></iframe></div> <script type="text/javascript"> document.all.mdv.style.pixelHeight = screen.height - 450; </script></td> </tr> </table> </body> </html> |