Reply
to scan sub directories
Old 06-30-2009, 06:39 AM to scan sub directories
Novice Talker

Posts: 14
Trades: 0
I can scan every file in the current directory, but im not sure how to tweak my code so it scans the sub directories of that directory as well.
If someone could point us in which part to change or add in the code and a guidence of how to do so would be greatly appreciated.

PHP Code:
$result = @mysql_query($selected);
if (!
$result
{
 
 echo 
"Database exists <br>";
 
 
mysql_query("DROP DATABASE WEBSEARCHER"
 or die(
mysql_error());
 if (
mysql_query("CREATE DATABASE websearcher"$dbh))
   {
   echo 
"New Database created <br>";
   }
 else
   {
   echo 
"Error creating database <p> ";
   } 
 
$selected mysql_select_db("websearcher"$dbh)
 or die(
"Could not select websearcher");
 
$sql_table1 "CREATE TABLE webdata
 (
 filename varchar(25),
 words varchar(25),
 PRIMARY KEY (filename,words)
 )"
;
 if(
mysql_query($sql_table1,$dbh))
 {
 print 
"new table created  <p>";
 }
 
}

function 
opening($file)
{
$stoplist $_SERVER['DOCUMENT_ROOT']."/worksheets/stoplist.txt";
$pattern "(\.txt$)|(\.php$)|(\.html$)|(\.htm$)";  
if(
eregi($pattern$file)){
 
$file_handle fopen($file"r");
 
$contents file($file);
 while (!
feof($file_handle)){
  
$data fgets($file_handle);
  
$words explode(' ' $data);
  
$duplicates array_unique($words);
  
sort($duplicates);
  if(
is_bool(strpos($stoplist'$duplicates[i]')))
  {
   foreach(
$duplicates as $word){
    
$word serialize($word);
    
$sql "INSERT INTO webdata VALUES('$file','$word')";
    if (
mysql_query($sql))
    {
     print 
"<i>successful</i> <br>";
    }
   }
  }

}
}
else {
 echo 
"Cant open that file type <p>"; }
}
 
function 
getfiles($dirname=".")
{
$file = array($file);
$subfiles = Array();
if(
$handle opendir($dirname)) {
 while(
false !== ($file readdir($handle))){
  echo 
"<b>$file</b> <br />";
  
opening($file);
  
  

}
closedir($handle);
}
return(
$file);
}
getfiles($_SERVER['DOCUMENT_ROOT']."/worksheets"); 
applebiz89 is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 06-30-2009, 07:28 AM Re: to scan sub directories
tripy's Avatar
Do not try this at home!

Posts: 3,139
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Simply make the getFiles() function recursive:
PHP Code:
<?php

function getfiles($dirname="."){
    
$aryFile = array();
    
$aryCurDir = array();
    
$arySub = array();
    
    if(
$handle opendir($dirname)) {
        while(
false !== ($file readdir($handle))){
            if( (
$file!='.') && ($file!='..') && (is_dir("$dirname/$file")) ){
                
//we found a subdirectory, we parse it and add to the current list of file
                
$arySub=getfiles("$dirname/$file");
                
$aryFile=array_merge($aryFile$arySub);
            }
            else{
                
$aryFile[]="$dirname/$file";
            }
        }
    
closedir($handle);
    }
    return(
$aryFile);
}

$ary=getfiles("/opt/firefox/");
echo 
"<pre>".var_dump($ary)."</pre>";
?>
Just a warning: the files are flatenned in the result of getFiles().
But you will find in the array the path relative to the root directory given to parse.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-30-2009, 08:08 AM Re: to scan sub directories
Novice Talker

Posts: 14
Trades: 0
hey, thanks. But from there how can I use the opening function to open each file and insert the contents into the database within getfiles function? or there another way of calling it.
applebiz89 is offline
Reply With Quote
View Public Profile
 
Old 06-30-2009, 09:12 AM Re: to scan sub directories
tripy's Avatar
Do not try this at home!

Posts: 3,139
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
I thought you would catch from there.
Simply do a foreach on the array:
PHP Code:
$files=getfiles(".");
foreach(
$files as $key=>$value){
  
opening($value);

It's as simple as that
:-))
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 06-30-2009 at 09:13 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to to scan sub directories
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.13153 seconds with 13 queries