Searching for a string in multiple files and folders

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
meemerz00
Forum Newbie
Posts: 5
Joined: Fri Jan 13, 2006 11:20 am

Searching for a string in multiple files and folders

Post by meemerz00 »

:roll:
I am trying to make a search page of sorts, where the user can type in various things such as a name, account number, etc., and it will search a specific folder that has several subfolders of pdf files. I need it to actually search inside the file, and not just the file name. It then needs to return all files that match the criteria, and the user can click on each of them to view them. These files aren't in a database, just in a directory on my web server. I really don't even know where to start. Can anyone help me out?
Thanks!!!
:D
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You could try a combination of `grep' and `strings' via the shell for the PDF stuff.

Untested principle only...

Code: Select all

$dir = './';
$search = 'foo'; //Word to search for
$files = array();
$handle = opendir($dir);
while ($file = readdir($handle))
{
    $command = 'strings '.$dir.$file.' | grep '.$search;
    $result = `$command`;
    if (!empty($result)) $files[] = $file;
}
closedir($handle);
print_r($files);
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

if memory serves:

Code: Select all

<?php

$files = explode("\r\n", shell_exec('grep -hrs ' . $searchstring . ' /starting/directory/'));

?>
That will recursively search for $searchstring in all files and sub-dirs in /starting/directory/ and suppress all messages except for matches and will return filenames.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Jenk wrote:if memory serves:

Code: Select all

<?php

$files = explode("\r\n", shell_exec('grep -hrs ' . $searchstring . ' /starting/directory/'));

?>
That will recursively search for $searchstring in all files and sub-dirs in /starting/directory/ and suppress all messages except for matches and will return filenames.
Hahaha... but mine looks better... OK maybe not :P I cant remember if grep is binary safe or not.
meemerz00
Forum Newbie
Posts: 5
Joined: Fri Jan 13, 2006 11:20 am

Post by meemerz00 »

That returns the word "Array." That's it.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

try print_r($files); instead of echo $files;
meemerz00
Forum Newbie
Posts: 5
Joined: Fri Jan 13, 2006 11:20 am

Post by meemerz00 »

I'm afraid I've tried both....any other ideas?
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

meemerz00 wrote:I'm afraid I've tried both....any other ideas?
That's probably because you don't have command-line access on your server, or it's a windows server.

You can try the Directory Functions to traverse each directory and file.
This will be pretty slow, so indexing might be a good idea.
meemerz00
Forum Newbie
Posts: 5
Joined: Fri Jan 13, 2006 11:20 am

Post by meemerz00 »

It's a Windows Server. I guess I don't understand what you mean by traversing the directories and files. I'm really new at this, sorry!!
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

print_r(explode("\n", shell_exec('find /I "test" *.*')));
Though you will have to manually go through all the directories as you can't use FIND recursively.
Post Reply