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 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s