Reading files in directory

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
winsonlee
Forum Commoner
Posts: 76
Joined: Thu Dec 11, 2003 8:49 pm

Reading files in directory

Post by winsonlee »

How can i program in a way that the php script able ot read a keyword and list all the files that contains the keyword ??

There is a page that that contain an input box that allows user to key in the keyword and after the user click on the button, it will try matching all the files that contain the keyword on the drive and list it on a page. Will it be possible to make the search function works just like the windows search function ??
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

glob()

Post by akimm »

I thought glob listed files in a directory, can it actually search the entirity of the file, I think he might need something different than glob()
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

oh my mistake... I thought he meant search for a keyword in the file name :oops: (guess I should have read the whole post)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

depending on the size of the files and the amount of RAM php is allowed to use, file()/file_get_contents() may be of interest. If the files are too large, fopen()/fread() and some clever substr() calls will be in order.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Well

Post by akimm »

Get someone better hopefully to correspond, or adjust this code, but basically perl regular expressions are good *I think* for your purpose.

Code: Select all

<?php
##b in front of web indicates specific search, not partial, words like webbling would not be found
##after the preg_ function I am kind of experimenting, because I don't know these functions well yet.
##assuming $_POST['search'] is the value for a search form..
if (preg_match("/\bweb\b/i", $_POST['search'] )) {
   echo "A match was found.";
} else {
   echo "A match was not found.";
}
##below it searches in-between those quotes for that phrase.  
if (preg_match("/\bweb\b/i", ".")) {
   echo "A match was found.";
} else {
   echo "A match was not found.";
}
?>
Post Reply