Jump to content


Photo

Directory Browsing


  • Please log in to reply
3 replies to this topic

#1 Moon-Man.net

Moon-Man.net
  • Members
  • PipPipPip
  • Advanced Member
  • 84 posts

Posted 05 September 2006 - 06:02 AM

Hey all,
I have a script that will replace any files in a given directory with banned Charactures, However, how can i make this script be reocursive? EG. will also replace files in subdirectories of the given dir. I am totally stuffed for that:S
Thanks heaps EVERYONE for their help!
Cheers,
Nathan

#!/usr/bin/php
<?PHP
$dir = '/tmp/TESTING';
$handle = opendir($dir);
$cnt=0;

echo "<------------Starting Replace!------------>\n" ;
echo "\n";
while(($file = readdir($handle)) !== false){
//	echo "File found: " .$file ."\n";
    if (preg_match('/[' .preg_quote('\/ : * ? " < > |', '/') .']/', $file)){
		echo "Bad file found: " .$file ."  " ;
		$cnt++ ;
        $path = $dir . DIRECTORY_SEPARATOR . $file;
        if (!in_array($file, array('..', '.')) && is_file($path)){
            $contents = file_get_contents($path);
            $newPath = $dir . DIRECTORY_SEPARATOR . preg_replace('/[' .preg_quote('\/ : * ? " < > |', '/') .']/', '', $file);
            echo "Replacing with " .$newPath ."\n" ;
//          if (file_put_contents($newPath, $contents) !== false){
//               unlink($path); //delete old file only if able to create new
//          }
        }
    }
}
echo "\n";
echo "\n";
echo "<------------Replace Finnished------------>\n" ;
echo "There were " .$cnt ." Replacements Made\n" ;
?>


#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 05 September 2006 - 07:20 AM

function replace_bad_files($dir) {
  $handle = opendir($dir);
  ...
  while (($file = readdir($handle)) !== false) {
    # If we found a directory, call ourselves again!  Simple
    $path = $dir . DIRECTORY_SEPARATOR . $file;
    if (is_dir($path)) {
      replace_bad_files($path);
      continue; # This will skip to the next file
    }
    ...
  }
}

The important steps here are

  • Make a function
  • Call the function recursively when you find a directory (as indicated by is_dir())

To count the global number of replacements, you can either use a global variable, or you can have your function return the count, by adding "return $cnt;" at the end.  In that case, the recursive call should be "$cnt += replace_bad_files($path)".

#3 Moon-Man.net

Moon-Man.net
  • Members
  • PipPipPip
  • Advanced Member
  • 84 posts

Posted 05 September 2006 - 02:07 PM

Awesome, so i can have a

if(is_dir($file)){
 replace_bad_file($file) ;
}
Somewhere in there, and it will keep opening all unlimited ammounts of functions untill it is totally re-ocursive :D
I diddnt know you call call the function you are in from within it :)
Thanks Heaps.
Cheers,
Nathan

#4 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 05 September 2006 - 02:28 PM

Use a static variable for counting, not global :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users