Търсачка на файлове

Soul_Stealler

Registered
Ето един код за търсачка на файлове. Направил съм го в два вариянта. Единия е с AJAX, другия е без.
с AJAX
без AJAX
Ето и картинката на папката:
folder.png

________________________________________________________________

Ето кода на търсачката без AJAX.
search.php
Код:
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<title>Search</title>
<style type="text/css">
body{

}
a:link {
	color: #0078cc;
	text-decoration: none;
}
a:visited {
	text-decoration: none;
	color: #0078cc;
}
a:hover {
	text-decoration: none;
	color: #eb7c02;
}

a:active {
	text-decoration: none;
	color: #0072cc;
}
.button {
border-color:#666699; 
background: #F2F2F2; 
color:#666699; 
font:11px Verdana,Arial,Helvetica,sans-serif;
border-width: 1px 1px 1px 1px;
padding:2px;
margin:0px;
}
.field {
border-color:#666699; 
background: #F2F2F2; 
color:#666699; 
font:11px Verdana,Arial,Helvetica,sans-serif;
border-width: 1px 1px 1px 1px;
padding:3px;
font-weight: bold;
margin:0px;
}
img{
	border:0px;
}
</style>
</head>

<body>

<form action="./search.php" method="get">
<b>Търсене:</b> <input type="text" name="s" class="field" size="65" value="<?php
$s = htmlspecialchars($_GET['s'], ENT_QUOTES);
 echo $s; ?>">
<input type="submit" value="Търси" class="button">
</form>




<?php



function size($path){
	$size = filesize($path);
	if($size >= 1073741824)
{
$size = round($size / 1073741824 * 100) / 100;
$size=substr($size,0 , 4);
$size = $size . " GB";
}
elseif($size >= 1048576)
{
$size = round($size / 1048576 * 100) / 100 ;
$size=substr($size,0 , 4);
$size = $size . " MB";
}
elseif($size >= 1024)
{
$size = round($size / 1024 * 100) / 100;
$size=substr($size,0 , 4);
$size = $size . " KB";
}
else
{
$size=substr($size,0 , 4);
$size = $size . " B";
}
echo $size;
}


 function show($dir){
global $i;
global $file;
global $dir_path;
global $full_path;
$s = htmlspecialchars($_GET['s'], ENT_QUOTES);
$directory = opendir($dir);
while ($files = readdir($directory))
{
if (($files != "..") AND ($files != ".")){
	$path = $dir.'/'.$files;
	if (!is_dir($path)){
		if (eregi($s, $files)) {
		$i++;
		$file[]=$files;
		$dir_path[]=$dir;
		$full_path[]=$path;
 		}
	}
	if(is_dir($path)) {
	show($path);
	}
}
}
}


if ($s != null){
show (".");
echo "<table>
	<tr>
	<td>
	
	<table width=\"580px\" align=\"left\" style=\"font-size:12px;font-family: Tahoma;\">
	<tr>
	<td style=\"border:1px solid #DBDBDB;\" colspan=\"3\">
	<div style=\"padding:1px; background-color: #F2F2F2; white-space:nowrap\">Резултати за: ".$s."</div>
	</td>
	</tr>
	<tr>
	<td style=\"border:1px solid #DBDBDB;\" colspan=\"3\">
	<div style=\"padding:1px; background-color: #F2F2F2; white-space:nowrap\">Намерени файлове: ".$i."</div>
	</td>
	</tr>
	</table>
	</td>
	</tr>
	<tr>
	<td>
	<table width=\"580px\" align=\"left\" style=\"font-size:12px;font-family: Tahoma;\">";
 
$num = count($file);
for ($i=0;$i<$num;$i++){
	 if (strlen($file[$i])>"100"){
	 $totchki='...';
	 } else {
	 $totchki='';
	 }
     $file[$i]=substr($file[$i],0 , "100"); 

	  		   echo "<tr>
			 <td style=\"border:1px solid #DBDBDB;\" width=\"20px\" align=\"center\">
			 <div style=\"padding:1px; background-color: #F2F2F2; white-space:nowrap;height:15px;\">
			 <a href=\"".$dir_path[$i]."\" onClick=\"this.target='_blank'\">
			 <img src=\"folder.png\" height=\"15px\" width=\"15px\"></a>
			 </div>
			 </td>
			 <td style=\"border:1px solid #DBDBDB;\" width=\"510px\" align=\"left\">
			 <div style=\"padding:1px; background-color: #F2F2F2; white-space:nowrap;height:15px;\" onmouseover=\"this.style.backgroundColor='#F9F9F9'\" onmouseout=\"this.style.backgroundColor='#F2F2F2'\">
			 <a href=\"".$full_path[$i]."\">".$file[$i].$totchki."</a>
			 </div>
			 </td>
			 <td style=\"border:1px solid #DBDBDB;\" width=\"50px\" align=\"center\">
			 <div style=\"padding:1px; background-color: #F2F2F2; white-space:nowrap; color: #0078cc;height:15px;\">";
			 size($full_path[$i]);
			 echo "</div></td></tr>"."\n";
	
}
echo "</table></td></tr></table>";
}
?>

</div>
</body>

</html>
________________________________________________________________

Търсачката с AJAX:
search.php
Код:
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<title>Search</title>
<script src="ajax.js"></script>
<style type="text/css">
body{

}
a:link {
	color: #0078cc;
	text-decoration: none;
}
a:visited {
	text-decoration: none;
	color: #0078cc;
}
a:hover {
	text-decoration: none;
	color: #eb7c02;
}

a:active {
	text-decoration: none;
	color: #0072cc;
}
.button {
border-color:#666699; 
background: #F2F2F2; 
color:#666699; 
font:11px Verdana,Arial,Helvetica,sans-serif;
border-width: 1px 1px 1px 1px;
padding:2px;
margin:0px;
}
.field {
border-color:#666699; 
background: #F2F2F2; 
color:#666699; 
font:11px Verdana,Arial,Helvetica,sans-serif;
border-width: 1px 1px 1px 1px;
padding:3px;
font-weight: bold;
margin:0px;
}
img{
	border:0px;
}
</style>
</head>

<body>
<b>Търсене:</b> <input type="text" name="s" class="field" size="65" onChange="show(this.value)">
<div id="txtHint"></div>
</div>
</body>

</html>

ajax.js
Код:
var xmlHttp

function show(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="show.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText
}
}

function GetXmlHttpObject()
{
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}

show.php
Код:
<?php
 header ('Content-Type: text/plain; charset=cp1251');


function size($path){
	$size = filesize($path);
	if($size >= 1073741824)
{
$size = round($size / 1073741824 * 100) / 100;
$size=substr($size,0 , 4);
$size = $size . " GB";
}
elseif($size >= 1048576)
{
$size = round($size / 1048576 * 100) / 100 ;
$size=substr($size,0 , 4);
$size = $size . " MB";
}
elseif($size >= 1024)
{
$size = round($size / 1024 * 100) / 100;
$size=substr($size,0 , 4);
$size = $size . " KB";
}
else
{
$size=substr($size,0 , 4);
$size = $size . " B";
}
echo $size;
}


 function show($dir){
global $i;
global $file;
global $dir_path;
global $full_path;
global $s;
$s = htmlspecialchars($_GET['q'], ENT_QUOTES);
$directory = opendir($dir);
while ($files = readdir($directory))
{
if (($files != "..") AND ($files != ".")){
	$path = $dir.'/'.$files;
	if (!is_dir($path)){
		if (eregi($s, $files)) {
		$i++;
		$file[]=$files;
		$dir_path[]=$dir;
		$full_path[]=$path;
 		}
	}
	if(is_dir($path)) {
	show($path);
	}
}
}
}

show (".");
echo "<table>
	<tr>
	<td>
	
	<table width=\"580px\" align=\"left\" style=\"font-size:12px;font-family: Tahoma;\">
	<tr>
	<td style=\"border:1px solid #DBDBDB;\" colspan=\"3\">
	<div style=\"padding:1px; background-color: #F2F2F2; white-space:nowrap\">Резултати за: ".$s."</div>
	</td>
	</tr>
	<tr>
	<td style=\"border:1px solid #DBDBDB;\" colspan=\"3\">
	<div style=\"padding:1px; background-color: #F2F2F2; white-space:nowrap\">Намерени файлове: ".$i."</div>
	</td>
	</tr>
	</table>
	</td>
	</tr>
	<tr>
	<td>
	<table width=\"580px\" align=\"left\" style=\"font-size:12px;font-family: Tahoma;\">";
 
$num = count($file);
for ($i=0;$i<$num;$i++){
	 if (strlen($file[$i])>"100"){
	 $totchki='...';
	 } else {
	 $totchki='';
	 }
     $file[$i]=substr($file[$i],0 , "100"); 

	  		   echo "<tr>
			 <td style=\"border:1px solid #DBDBDB;\" width=\"20px\" align=\"center\">
			 <div style=\"padding:1px; background-color: #F2F2F2; white-space:nowrap;height:15px;\">
			 <a href=\"".$dir_path[$i]."\" onClick=\"this.target='_blank'\">
			 <img src=\"folder.png\" height=\"15px\" width=\"15px\"></a>
			 </div>
			 </td>
			 <td style=\"border:1px solid #DBDBDB;\" width=\"510px\" align=\"left\">
			 <div style=\"padding:1px; background-color: #F2F2F2; white-space:nowrap;height:15px;\" onmouseover=\"this.style.backgroundColor='#F9F9F9'\" onmouseout=\"this.style.backgroundColor='#F2F2F2'\">
			 <a href=\"".$full_path[$i]."\">".$file[$i].$totchki."</a>
			 </div>
			 </td>
			 <td style=\"border:1px solid #DBDBDB;\" width=\"50px\" align=\"center\">
			 <div style=\"padding:1px; background-color: #F2F2F2; white-space:nowrap; color: #0078cc;height:15px;\">";
			 size($full_path[$i]);
			 echo "</div></td></tr>"."\n";
	
}
echo "</table></td></tr></table>";

?>


Е, добре ли е станало ? :roll:
 
Soul_Stealler каза:
Ето един код за търсачка на файлове. Направил съм го в два вариянта. Единия е с AJAX, другия е без.
с AJAX
без AJAX
Ето и картинката на папката:
folder.png

________________________________________________________________

Ето кода на търсачката без AJAX.
search.php
Код:
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<title>Search</title>
<style type="text/css">
body{

}
a:link {
	color: #0078cc;
	text-decoration: none;
}
a:visited {
	text-decoration: none;
	color: #0078cc;
}
a:hover {
	text-decoration: none;
	color: #eb7c02;
}

a:active {
	text-decoration: none;
	color: #0072cc;
}
.button {
border-color:#666699; 
background: #F2F2F2; 
color:#666699; 
font:11px Verdana,Arial,Helvetica,sans-serif;
border-width: 1px 1px 1px 1px;
padding:2px;
margin:0px;
}
.field {
border-color:#666699; 
background: #F2F2F2; 
color:#666699; 
font:11px Verdana,Arial,Helvetica,sans-serif;
border-width: 1px 1px 1px 1px;
padding:3px;
font-weight: bold;
margin:0px;
}
img{
	border:0px;
}
</style>
</head>

<body>

<form action="./search.php" method="get">
<b>Търсене:</b> <input type="text" name="s" class="field" size="65" value="<?php
$s = htmlspecialchars($_GET['s'], ENT_QUOTES);
 echo $s; ?>">
<input type="submit" value="Търси" class="button">
</form>




<?php



function size($path){
	$size = filesize($path);
	if($size >= 1073741824)
{
$size = round($size / 1073741824 * 100) / 100;
$size=substr($size,0 , 4);
$size = $size . " GB";
}
elseif($size >= 1048576)
{
$size = round($size / 1048576 * 100) / 100 ;
$size=substr($size,0 , 4);
$size = $size . " MB";
}
elseif($size >= 1024)
{
$size = round($size / 1024 * 100) / 100;
$size=substr($size,0 , 4);
$size = $size . " KB";
}
else
{
$size=substr($size,0 , 4);
$size = $size . " B";
}
echo $size;
}


 function show($dir){
global $i;
global $file;
global $dir_path;
global $full_path;
$s = htmlspecialchars($_GET['s'], ENT_QUOTES);
$directory = opendir($dir);
while ($files = readdir($directory))
{
if (($files != "..") AND ($files != ".")){
	$path = $dir.'/'.$files;
	if (!is_dir($path)){
		if (eregi($s, $files)) {
		$i++;
		$file[]=$files;
		$dir_path[]=$dir;
		$full_path[]=$path;
 		}
	}
	if(is_dir($path)) {
	show($path);
	}
}
}
}


if ($s != null){
show (".");
echo "<table>
	<tr>
	<td>
	
	<table width="580px" align="left" style="font-size:12px;font-family: Tahoma;">
	<tr>
	<td style="border:1px solid #DBDBDB;" colspan="3">
	<div style="padding:1px; background-color: #F2F2F2; white-space:nowrap">Резултати за: ".$s."</div>
	</td>
	</tr>
	<tr>
	<td style="border:1px solid #DBDBDB;" colspan="3">
	<div style="padding:1px; background-color: #F2F2F2; white-space:nowrap">Намерени файлове: ".$i."</div>
	</td>
	</tr>
	</table>
	</td>
	</tr>
	<tr>
	<td>
	<table width="580px" align="left" style="font-size:12px;font-family: Tahoma;">";
 
$num = count($file);
for ($i=0;$i<$num;$i++){
	 if (strlen($file[$i])>"100"){
	 $totchki='...';
	 } else {
	 $totchki='';
	 }
     $file[$i]=substr($file[$i],0 , "100"); 

	  		   echo "<tr>
			 <td style="border:1px solid #DBDBDB;" width="20px" align="center">
			 <div style="padding:1px; background-color: #F2F2F2; white-space:nowrap;height:15px;">
			 <a href="".$dir_path[$i]."" onClick="this.target='_blank'">
			 <img src="folder.png" height="15px" width="15px"></a>
			 </div>
			 </td>
			 <td style="border:1px solid #DBDBDB;" width="510px" align="left">
			 <div style="padding:1px; background-color: #F2F2F2; white-space:nowrap;height:15px;" onmouseover="this.style.backgroundColor='#F9F9F9'" onmouseout="this.style.backgroundColor='#F2F2F2'">
			 <a href="".$full_path[$i]."">".$file[$i].$totchki."</a>
			 </div>
			 </td>
			 <td style="border:1px solid #DBDBDB;" width="50px" align="center">
			 <div style="padding:1px; background-color: #F2F2F2; white-space:nowrap; color: #0078cc;height:15px;">";
			 size($full_path[$i]);
			 echo "</div></td></tr>"."\n";
	
}
echo "</table></td></tr></table>";
}
?>

</div>
</body>

</html>
________________________________________________________________

Търсачката с AJAX:
search.php
Код:
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<title>Search</title>
<script src="ajax.js"></script>
<style type="text/css">
body{

}
a:link {
	color: #0078cc;
	text-decoration: none;
}
a:visited {
	text-decoration: none;
	color: #0078cc;
}
a:hover {
	text-decoration: none;
	color: #eb7c02;
}

a:active {
	text-decoration: none;
	color: #0072cc;
}
.button {
border-color:#666699; 
background: #F2F2F2; 
color:#666699; 
font:11px Verdana,Arial,Helvetica,sans-serif;
border-width: 1px 1px 1px 1px;
padding:2px;
margin:0px;
}
.field {
border-color:#666699; 
background: #F2F2F2; 
color:#666699; 
font:11px Verdana,Arial,Helvetica,sans-serif;
border-width: 1px 1px 1px 1px;
padding:3px;
font-weight: bold;
margin:0px;
}
img{
	border:0px;
}
</style>
</head>

<body>
<b>Търсене:</b> <input type="text" name="s" class="field" size="65" onChange="show(this.value)">
<div id="txtHint"></div>
</div>
</body>

</html>

ajax.js
Код:
var xmlHttp

function show(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="show.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText
}
}

function GetXmlHttpObject()
{
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}

show.php
Код:
<?php
 header ('Content-Type: text/plain; charset=cp1251');


function size($path){
	$size = filesize($path);
	if($size >= 1073741824)
{
$size = round($size / 1073741824 * 100) / 100;
$size=substr($size,0 , 4);
$size = $size . " GB";
}
elseif($size >= 1048576)
{
$size = round($size / 1048576 * 100) / 100 ;
$size=substr($size,0 , 4);
$size = $size . " MB";
}
elseif($size >= 1024)
{
$size = round($size / 1024 * 100) / 100;
$size=substr($size,0 , 4);
$size = $size . " KB";
}
else
{
$size=substr($size,0 , 4);
$size = $size . " B";
}
echo $size;
}


 function show($dir){
global $i;
global $file;
global $dir_path;
global $full_path;
global $s;
$s = htmlspecialchars($_GET['q'], ENT_QUOTES);
$directory = opendir($dir);
while ($files = readdir($directory))
{
if (($files != "..") AND ($files != ".")){
	$path = $dir.'/'.$files;
	if (!is_dir($path)){
		if (eregi($s, $files)) {
		$i++;
		$file[]=$files;
		$dir_path[]=$dir;
		$full_path[]=$path;
 		}
	}
	if(is_dir($path)) {
	show($path);
	}
}
}
}

show (".");
echo "<table>
	<tr>
	<td>
	
	<table width="580px" align="left" style="font-size:12px;font-family: Tahoma;">
	<tr>
	<td style="border:1px solid #DBDBDB;" colspan="3">
	<div style="padding:1px; background-color: #F2F2F2; white-space:nowrap">Резултати за: ".$s."</div>
	</td>
	</tr>
	<tr>
	<td style="border:1px solid #DBDBDB;" colspan="3">
	<div style="padding:1px; background-color: #F2F2F2; white-space:nowrap">Намерени файлове: ".$i."</div>
	</td>
	</tr>
	</table>
	</td>
	</tr>
	<tr>
	<td>
	<table width="580px" align="left" style="font-size:12px;font-family: Tahoma;">";
 
$num = count($file);
for ($i=0;$i<$num;$i++){
	 if (strlen($file[$i])>"100"){
	 $totchki='...';
	 } else {
	 $totchki='';
	 }
     $file[$i]=substr($file[$i],0 , "100"); 

	  		   echo "<tr>
			 <td style="border:1px solid #DBDBDB;" width="20px" align="center">
			 <div style="padding:1px; background-color: #F2F2F2; white-space:nowrap;height:15px;">
			 <a href="".$dir_path[$i]."" onClick="this.target='_blank'">
			 <img src="folder.png" height="15px" width="15px"></a>
			 </div>
			 </td>
			 <td style="border:1px solid #DBDBDB;" width="510px" align="left">
			 <div style="padding:1px; background-color: #F2F2F2; white-space:nowrap;height:15px;" onmouseover="this.style.backgroundColor='#F9F9F9'" onmouseout="this.style.backgroundColor='#F2F2F2'">
			 <a href="".$full_path[$i]."">".$file[$i].$totchki."</a>
			 </div>
			 </td>
			 <td style="border:1px solid #DBDBDB;" width="50px" align="center">
			 <div style="padding:1px; background-color: #F2F2F2; white-space:nowrap; color: #0078cc;height:15px;">";
			 size($full_path[$i]);
			 echo "</div></td></tr>"."\n";
	
}
echo "</table></td></tr></table>";

?>


Е, добре ли е станало ? :roll:

Значи тази търсачка търси в директориите с файлове на сайта така ли да разбирам, че ми трябва нещо такова.......
---------------------
що не го пуснеш като урок!!!!????
---------------------
какво е Ajax ???
 
SandPrince, да, това търси за файлове на сървъра, само че в предварително зададена папка и нейните СУБ папки. :wink:
Не искам да го пускам все още като урок, защото имам още идеи за тази търсачка ;) Файловете ще се показват по 30-40-50 на страница и т.н. :wink:

human, това не го разбрах :)
много можеше да сложиш бутон на "демото"
 
Защото не ти трябва бутон ;)
Като нанасяш букви моментално би трябвало да ти намира файловете ;)
само че нещо май не съм го направил като хората. Натисни Enter и ще видиш как ще ти намери файловете без да се презареди страницата :)
 
Soul_Stealler браво много добра търсачка :?: обаче аз имам един проблем исках да сложа форма select за да може да се избира в коя директория да се търси но не намерих решение на проблема може би защото все още съм начинаещ мисля че трябва да стане с масиви може ли да помогнеш за това :evil:
Тук съм пробвал да го направя но засега бачка по следния начин: копирал съм search.php там кадето искам да се търси и съм сложил линкове към тях :wink:
 
Напишете думите които търсите и натиснете някъде отстрани в полето например, от js-то ви показва прозорче под полето за търсене, намерените резултати. :)
 

Горе