?
Solved

PHP Code to Search Files in a Folder

Posted on 2009-05-06
16
Medium Priority
?
264 Views
Last Modified: 2012-06-21
Hello,

I'm looking for a PHP code which enables me search files in a folder.

For example I have two folders say Docs & Music. Now I search for a keyword say 'John', then the script would be able to search that John keyword from both folders.


Thanks in Advance
0
Comment
Question by:Andy1988
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 2
  • +2
16 Comments
 
LVL 6

Expert Comment

by:Mushq
ID: 24322709
For more details please check.
http://www.phpjabbers.com/ask91-read-files-name-in-folder-using-php.html

Best Regards,
Mushi
<?php
$dir = ".";
 
$dh = opendir($dir);
 
while (($file = readdir($dh)) !== false) {
echo $file;
}
 
closedir($dh);
?> 

Open in new window

0
 
LVL 5

Expert Comment

by:drakeshe
ID: 24322718
Here you go.
<?php
	$find = "john";
	if ($handle = opendir('/path/to/files')) {
	    while ($file = readdir($handle)) {
        echo "$file\n";
		if(strstr($file, $find){
			echo("Found file containing ".$find);
			echo($file);
		}
    }
    closedir($handle);
?>

Open in new window

0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24322762
Hi,

Look at theese:

http://in2.php.net/readdir

This will help you.

Regards
0
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 
LVL 14

Expert Comment

by:shobinsun
ID: 24322777
0
 
LVL 4

Author Comment

by:Andy1988
ID: 24322891
Well, I guess you got me wrong. I want a code to search file not to list the file:

A search box with a button and the code can show the results of the keyword which has been entered in the searchbox.
0
 
LVL 4

Author Comment

by:Andy1988
ID: 24322923
@drakeshe: I'm getting this error in the code:
Parse error: syntax error, unexpected '{' in /home/webinco/public_html/demo/search.php on line 6

Open in new window

0
 
LVL 5

Expert Comment

by:drakeshe
ID: 24322989
Fixed
<?php
	$find = "john";
	if ($handle = opendir('/path/to/files')) {
		while ($file = readdir($handle)) {
			if(strstr($file, $find)){
					echo("Found file containing ".$find);
					echo($file);
			}
		}
	}
    closedir($handle);
?>

Open in new window

0
 
LVL 5

Expert Comment

by:drakeshe
ID: 24323002
Edit:

<?php
	$find = "jogn";
	if ($handle = opendir('music/')) {
		while ($file = readdir($handle)) {
			if(strstr($file, $find)){
				echo("Found file containing ".$find."<BR>");
				echo($file);
			}
		}
	}
    closedir($handle);
?>

Open in new window

0
 
LVL 4

Author Comment

by:Andy1988
ID: 24323011
Okay this script works for me: now please tell me how can I make it a search landing page?

I mean what should I do to work this code with search page/field?
0
 
LVL 7

Expert Comment

by:ycTIN
ID: 24323023
FYI
http://php.net/glob
<?php
$find = "test";
$dir = "/Doc/"; // 
 
foreach (glob("$dir*$find*") as $filename) {
    echo "$filename, size " . filesize($filename) . "\n";
}
?>

Open in new window

0
 
LVL 5

Expert Comment

by:drakeshe
ID: 24323041
I better get A++ =P


<?php
	$submitted = @$_POST['submit'];
	if ($submitted != '') {
		$find = $_POST['keyword'];
		if ($handle = opendir('images/')) {
			while ($file = readdir($handle)) {
				if(strstr($file, $find)){
					echo("Found file containing ".$find."<BR>");
					echo($file);
				}
			}
		}
		closedir($handle);
	}
?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
	<table width="auto" border="0">
		<tr>
			<td colspan="3"><strong>Find Song: </strong></td>
		</tr>
		<tr>
			<td height="25">Song Name Part:</td>
			<td><label><input name="keyword" type="text" size="39"></label></td>
			<td>eg. John</td>
		</tr>	
		<tr>
		    <td height="25"><label><input type="submit" name="submit" value="Find!" /></label></td>
		    <td>&nbsp;</td>
		</tr>
	</table>
</form>

Open in new window

0
 
LVL 5

Expert Comment

by:drakeshe
ID: 24323061
Using ycTIN's method:
<?php
	$submitted = @$_POST['submit'];
	if ($submitted != '') {
		$find = $_POST['keyword'];
		$find = $_POST['keyword'];
		$dir = "/Doc/";	
		foreach (glob("$dir*$find*") as $filename) {
			echo("Found file containing ".$find."<BR>");
			echo($filename."<BR>");
		}
		closedir($handle);
	}
?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
	<table width="auto" border="0">
		<tr>
			<td colspan="3"><strong>Find Song: </strong></td>
		</tr>
		<tr>
			<td height="25">Song Name Part:</td>
			<td><label><input name="keyword" type="text" size="39"></label></td>
			<td>eg. John</td>
		</tr>	
		<tr>
		    <td height="25"><label><input type="submit" name="submit" value="Find!" /></label></td>
		    <td>&nbsp;</td>
		</tr>
	</table>
</form>

Open in new window

0
 
LVL 4

Author Comment

by:Andy1988
ID: 24323105
Thanks, you'd definitely get A++ :D

Btw, what code should I add to show file not found if the file is not there. :)

Sorry but I don't have knowledge about PHP :(
0
 
LVL 5

Accepted Solution

by:
drakeshe earned 2000 total points
ID: 24323118
Added "No files found"
<?php
	$submitted = @$_POST['submit'];
	if ($submitted != '') {
		$find = $_POST['keyword'];
		$dir = "images/";	
		$found = false;
		foreach (glob("$dir*$find*") as $filename) {
			echo("Found file containing ".$find."<BR>");
			echo($filename."<BR>");
			$found = true;
		}
		if($found == false){
			echo("No Files have been found");
		}
	}
?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
	<table width="auto" border="0">
		<tr>
			<td colspan="3"><strong>Find Song: </strong></td>
		</tr>
		<tr>
			<td height="25">Song Name Part:</td>
			<td><label><input name="keyword" type="text" size="39"></label></td>
			<td>eg. John</td>
		</tr>	
		<tr>
		    <td height="25"><label><input type="submit" name="submit" value="Find!" /></label></td>
		    <td>&nbsp;</td>
		</tr>
	</table>
</form>

Open in new window

0
 
LVL 4

Author Comment

by:Andy1988
ID: 24323252
Nice,

Its working now.

However I applied this code to the code that you had given me earlier:
<?php
        $submitted = @$_POST['submit'];
        if ($submitted != '') {
                $find = $_POST['keyword'];
                $found = false;
                if ($handle = opendir('.')) {
                        while ($file = readdir($handle)) {
                                if(strstr($file, $find)){
                                        echo("Found file containing ".$find."<BR>");
                                        echo($file);
                                        $found = true;
                                }
                                }
                                if($found == false){
                        echo("No Files have been found");
                       }
                }
                closedir($handle);
        }
?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <table width="auto" border="0">
                <tr>
                        <td colspan="3"><strong>Find Song: </strong></td>
                </tr>
                <tr>
                        <td height="25">Song Name Part:</td>
                        <td><label><input name="keyword" type="text" size="39"></label></td>
                        <td>eg. John</td>
                </tr>   
                <tr>
                    <td height="25"><label><input type="submit" name="submit" value="Find!" /></label></td>
                    <td> </td>
                </tr>
        </table>
</form>

Open in new window

0
 
LVL 4

Author Closing Comment

by:Andy1988
ID: 31578873
Thanks for the quick help.
0

Featured Post

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question