Search files in folders and subfolders in php

Guys,

I am back..with couple of hours working on file search in directories and sub directories..i figured on my own. Its pretty easy to search a file in directories and sub directories at a time.

A small template design to show search text box is already posted in my previous post

Lets go directly to the main code..here is the snippet..

Code Snippet:

$strsear=$_POST[“stringsearch”];
$i=1;$count=0;
$path = realpath(‘C:\wamp\www\apps’);
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename)
{
$resultspath[$i]=$filename->getPathname();
foreach( $resultspath as $value)
{
if(stripos($value,$strsear)!==false){
echo $value.”<br/>”;
$count++;
}
}

}

Output will display filename that are existed in directory and sub-directory.

Folder structure:

../apps/zend.jpg

../apps/img/zend.jpg

 

Say i was trying to find a “zend.jpg” file from the above folder structure..just type zend and hit enter..now you can view 2 paths as listed above. Hope it helps you.. 😀

Stay tuned to sathyalog 🙂

Search engine to search files in folders

Hey guys,

Today we will go through a simple algorithm to search files in a folder.

Lets search in a different way..kinda like if we are searching for a image we will mention image name and click on image radio button and then search. Similary docs,audios,videos,other files etc etc.

Hope our simple mission is clear..lets see how..dont panic folks..its pretty simple function which satisfies our requirement clearly. The function is glob() which has been introduced in PHP4.

First design a simple template with textbox to search and radio buttons for selecting options. I hope changing css is not a big thing once we are done with functionality then we can process into it.

filesearch.php:

<form id=”form1″ name=”form1″ method=”post” action=”searchresult.php”>
<label>Search
<input type=”text” name=”stringsearch” />
<br />
<br />
<input name=”radiobutton” type=”radio” value=”image” />
Images
<input name=”radiobutton” type=”radio” value=”doc” />
doc/docx
<input name=”radiobutton” type=”radio” value=”ppt” />
ppt/pptx
<input name=”radiobutton” type=”radio” value=”xls” />
xls/xlsx
<input name=”radiobutton” type=”radio” value=”audio” />
audio
<input name=”radiobutton” type=”radio” value=”video” />
video
<input name=”radiobutton” type=”radio” value=”others” />
others<br />
<br />
</label>
<label>
<input type=”submit” name=”Submit” value=”Submit” />
</label>
</form>

07-02-2014 09-56-32

searchresult.php:

<?php
$dir=”C:\wamp\www\apps”;

$strsear=$_POST[“stringsearch”];
$radio=$_POST[“radiobutton”];
switch ($radio)
{
case “image”:
$result=$strsear.”*.jpg”;

break;
case “doc”:
//similarly doc
break;
case “ppt”:
//similarly ppt
break;
case “xls”:
//similarly xls
break;
case “audio”:
//similarly audio
break;
case “video”:
//similarly video
break;
default:
$result=$strsear.”*.php”; //list anyother files
}
foreach(glob($dir.DIRECTORY_SEPARATOR.$result) as $filename){

echo $filename.”<br/>”;
}
?>

This is a simple prototype to achieve only for a simple search of a filename in folders. In my next post i will share how to search files in folders and subfolders clearly. Hope you like it 🙂