dsaba Posted April 19, 2007 Share Posted April 19, 2007 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 Link to comment https://forums.phpfreaks.com/topic/47674-help-with-some-pseudo-code-for-alogorithim/ Share on other sites More sharing options...
Glyde Posted April 19, 2007 Share Posted April 19, 2007 <?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. Link to comment https://forums.phpfreaks.com/topic/47674-help-with-some-pseudo-code-for-alogorithim/#findComment-232810 Share on other sites More sharing options...
dsaba Posted April 19, 2007 Author Share Posted April 19, 2007 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 Link to comment https://forums.phpfreaks.com/topic/47674-help-with-some-pseudo-code-for-alogorithim/#findComment-232824 Share on other sites More sharing options...
Glyde Posted April 19, 2007 Share Posted April 19, 2007 As long as the files are stored locally on your computer, you can use file_get_contents with no problem on a .php file, you'll receive the source code. Link to comment https://forums.phpfreaks.com/topic/47674-help-with-some-pseudo-code-for-alogorithim/#findComment-232827 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.