9, November 2009

Webdigity tutorials : Search a code fragment in a directory recursive

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Tutorials @ webDigity
Search a code fragment in a directory recursive

Subscribe to our tutorials feed!

Some times we need to search the contents of many files through php. Personally I wrote this piece of code when I was using eaccelerator, and used it to search the whole server for files that are not compiled.

Please mention that the class bellow, will perform a recursive search. So it will even parse subdirectories of the directory you will use.

Here is the main class:
Code:
<?php
class searchFileContents{
    var 
$dir_name '';//The directory to search
    
var $search_phrase '';//The phrase to search in the file contents
    
var $allowed_file_types = array('php','phps');//The file types that are searched
    
var $foundFiles;//Files that contain the search phrase will be stored here
    
var $myfiles;
    
    function 
search($directory$search_phrase){
        
$this->dir_name $directory;
        
$this->search_phrase $search_phrase;
        
        
$this->myfiles $this->GetDirContents($this->dir_name);
        
$this->foundFiles = array();
        
        if ( empty(
$this->search_phrase) ) die('Empty search phrase');
        if ( empty(
$this->dir_name) ) die('You must select a directory to search');
        
        foreach ( 
$this->myfiles as $f ){
            if ( 
in_array(array_pop(explode '.'$f )),  $this->allowed_file_types) ){
                
$contents file_get_contents($f);
                if ( 
strpos($contents$this->search_phrase) !== false )
                    
$this->foundFiles [] = $f;
            }
        }
        return 
$this->foundFiles;
    }
    
    function 
GetDirContents($dir){
       if (!
is_dir($dir)){die ("Function GetDirContents: Problem reading : $dir!");}
       if (
$root=@opendir($dir)){
           while (
$file=readdir($root)){
               if(
$file=="." || $file==".."){continue;}
               if(
is_dir($dir."/".$file)){
                   
$files=array_merge($files,$this->GetDirContents($dir."/".$file));
               }else{
               
$files[]=$dir."/".$file;
               }
           }
       }
       return 
$files;
    }
}
?>


And here is an example of usage:

Code:
$search = new searchFileContents;
$search->search('E:/', 'class');
var_dump($search->foundFiles);

In the above example we will search the whole E: disk for the word "class". Please mention the allowed_file_types property where you can put the file types that you want to be parsed. In our example we search only php and phps files.

The results are stored in the foundFiles property (array). Hope you liked this snippet :)
Do you like this tutorial?
Feel free to share it with others using Dzone or your favorite bookmarking service. You can see related discussions in our php forum
This tutorial posted by Nikolas from Sublime Directory

Sponsors :

Code library menu :
[ Tutorials home | Submit a tutorial ]

User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Nov 09, 2009, 06:59:41 am





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 42.741
Total Topics: 8.375
Total Members: 6.939
Tutorials : 58
Resources : 929
Designs : 341
Latest Member: Auguste Dubuisson

24 Guests, 3 Users online :

15 users online today:




Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2009 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.