Search files in folders and subfolders in php


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 is the snippet..

Code Snippet:

$path = realpath(‘C:\wamp\www\apps’);
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename)
foreach( $resultspath as $value)
echo $value.”<br/>”;


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

Folder structure:




Say i was trying to find a “zend.jpg” file from the above folder structure..just type zend and hit 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.


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

07-02-2014 09-56-32



switch ($radio)
case “image”:

case “doc”:
//similarly doc
case “ppt”:
//similarly ppt
case “xls”:
//similarly xls
case “audio”:
//similarly audio
case “video”:
//similarly video
$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 🙂