Jump to content

help with some pseudo-code for alogorithim


dsaba

Recommended Posts

i'm trying to create a script which will look in a / root directory

1. in each subdirectory find all .php files

2. rename all .php files to .phps

3. let me make some changes to the .phps files, then rename back to .php

 

i need help with a starting algorithim for this and with my pseudocode for it

here's my attempt:

-----------------------------------------------------------

looking in "/" (root)

identify all subdirectories names (example: "/images")

put all subdirectory names in an array

 

foreach $value in the array

find all ".php" file extensions

rename to ".phps"

make some changes to each .phps file

rename back to ".php"

 

end

 

i get the process of finding directories and putting those into an array, but then inside each directory i will have to find multiple .php files, this is where I get confused on how to structure it

because do I put the filenames with .php in another array? and what about the file directory array? when i start referencing two arrays and looping inside loops i get confused

 

thanks

<?php
function processDirectory($directory) {
     $dHandle = opendir($directory);
     while (($file = readdir($dHandle)) !== FALSE) {
          if (in_array($file, array(".", ".."))) continue;
          $file = $directory . DIRECTORY_SEPARATOR . $file;
          if (is_dir($file)) processDirectory($file);
          elseif (strtolower(end(explode(chr(46), $file))) == "php") {
               // Do whatever work you need to do ot the file here
               //rename($file, substr($file, 0, -3) . "phps");
          }
     }
}
?>

Untested, but it should work.  Anyways, keep in mind that any major file alterations are dangerous, which is why I commented out the rename() function.  Only uncomment it when you are sure everything else is working fine.  Remember, be careful.  Although this will do nothing more than rename files, like I said...mass file manipulation is dangerous.

thanks for the help i'll try that out, and study the loops

 

the reason i want to rename .php to .phps is because

I'm manipulating .php files with file_get_contents()

 

if you know a better way to manipulate .php files with file_get_contents() other than renaming it to .phps so I can actually retrieve the source code

 

please tell me

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.