blueman378 Posted March 6, 2008 Share Posted March 6, 2008 hi guys, well i have this code <?php // Retrieve modules $modulepath = "."; $sectionListIgnore = array ('.','..','template.module','do.php'); $dh = opendir($modulepath); while (false !== ($file = readdir($dh))) { if(!in_array($file,$sectionListIgnore)){ $fh = fopen($file, 'r+'); $contents = fread($fh, filesize($file)); $new_contents1 = str_replace("FCK", "", $contents); $new_contents = str_replace("FCK", "", $new_contents1); fclose($fh); // Open file to write $fh = fopen($file, 'w+'); fwrite($fh, $new_contents); fclose($fh); } } closedir($dh); ?><table><?php // Retrieve modules $module2path = "."; $sectionListIgnore = array ('.','..','template.module','do.php'); $dh = opendir($module2path); while (false !== ($filed = readdir($dh))) { if(!in_array($filed,$sectionListIgnore)){ $files = str_replace("FCK", "", $filed); $fileg = str_replace("fck", "", $files); rename("$filed", "$fileg"); echo "<tr><td>"; echo $filed; echo "</td><td> ==> </td><td>"; echo $fileg; echo "</td></tr>"; } } closedir($dh); ?> </table> well this code works fine if i copy and paste it into indervidual directories but i have over 100 folders so need this to be recursive, any help would be appreciated Link to comment https://forums.phpfreaks.com/topic/94661-making-a-code-recursive/ Share on other sites More sharing options...
rajivgonsalves Posted March 6, 2008 Share Posted March 6, 2008 try this function for listing files recursively you can merge it with your function then ### function function ListFiles($dir) { if($dh = opendir($dir)) { $files = Array(); $inner_files = Array(); while($file = readdir($dh)) { if($file != "." && $file != ".." && $file[0] != '.') { if(is_dir($dir . "/" . $file)) { $inner_files = ListFiles($dir . "/" . $file); if(is_array($inner_files)) $files = array_merge($files, $inner_files); } else { array_push($files, $dir . "/" . $file); } } } closedir($dh); return $files; } } Link to comment https://forums.phpfreaks.com/topic/94661-making-a-code-recursive/#findComment-484690 Share on other sites More sharing options...
blueman378 Posted March 6, 2008 Author Share Posted March 6, 2008 heres the tidier code: <?php echo "<table>"; // remove string in files $modulepath = "."; $sectionListIgnore = array ('.','..','template.module','do.php'); $dh = opendir($modulepath); while (false !== ($file = readdir($dh))) { if(!in_array($file,$sectionListIgnore)){ $fh = fopen($file, 'r+'); $contents = fread($fh, filesize($file)); $new_contents1 = str_replace("fck", "", $contents); $new_contents = str_replace("FCK", "", $new_contents1); $fh = fopen($file, 'w+'); fwrite($fh, $new_contents); $files = str_replace("FCK", "", $file); $fileg = str_replace("fck", "", $files); rename("$file", "$fileg"); echo "<tr><td>"; echo $file; echo "</td><td> ==> </td><td>"; echo $fileg; echo "</td></tr>"; fclose($fh); // Open file to write fclose($fh); } } closedir($dh); echo "</table>"; ?> thanks Link to comment https://forums.phpfreaks.com/topic/94661-making-a-code-recursive/#findComment-484699 Share on other sites More sharing options...
blueman378 Posted March 6, 2008 Author Share Posted March 6, 2008 heres the code rewrote <?php echo "<table>"; // remove string in files $path = "."; $IgnoreList = array ('.','..','template.module','do.php'); $Replace = array ('FCK','fck'); $dh = opendir($path); while (false !== ($file = readdir($dh))) { if(!in_array($file,$IgnoreList)){ //remove the string from the file $fh = fopen($file, 'r+'); $contents = fread($fh, filesize($file)); $new_contents = str_replace($Replace, "", $contents); $fh = fopen($file, 'w+'); fwrite($fh, $new_contents); fclose($fh); //rename the files/folders $Filenew = str_replace($Replace, "", $file); rename("$file", "$Filenew"); echo "<tr> <td>". $file ."</td> <td> ==> </td> <td>". $Filenew ."</td> </tr>"; } } closedir($dh); echo "</table>"; ?> cheers Link to comment https://forums.phpfreaks.com/topic/94661-making-a-code-recursive/#findComment-484708 Share on other sites More sharing options...
blueman378 Posted March 6, 2008 Author Share Posted March 6, 2008 heres what im trying <?php ### function function ListFiles($dir) { if($dh = opendir($dir)) { $files = Array(); $inner_files = Array(); while($file = readdir($dh)) { if($file != "." && $file != ".." && $file[0] != '.') { if(is_dir($dir . "/" . $file)) { $inner_files = ListFiles($dir . "/" . $file); if(is_array($inner_files)) $files = array_merge($files, $inner_files); } else { array_push($files, $dir . "/" . $file); } } } closedir($dh); return $files; } } ListFiles($dir); //main code start echo "<table>"; // remove string in files $IgnoreList = array ('.','..','template.module','do.php'); $Replace = array ('FCK','fck'); $dh = opendir($path); foreach ($arrFiles as $strKey => $file) { if(!in_array($file,$IgnoreList)){ //remove the string from the file $fh = fopen($file, 'r+'); $contents = fread($fh, filesize($file)); $new_contents = str_replace($Replace, "", $contents); $fh = fopen($file, 'w+'); fwrite($fh, $new_contents); fclose($fh); //rename the files/folders $Filenew = str_replace($Replace, "", $file); rename("$file", "$Filenew"); echo "<tr> <td>". $file ."</td> <td> ==> </td> <td>". $Filenew ."</td> </tr>"; } } closedir($dh); echo "</table>"; ?> but i get Warning: Invalid argument supplied for foreach() in C:\Documents and Settings\Matthew\My Documents\Web\Thissite\editor\test\do.php on line 33 Warning: closedir(): supplied argument is not a valid Directory resource in C:\Documents and Settings\Matthew\My Documents\Web\Thissite\editor\test\do.php on line 53 cheers Link to comment https://forums.phpfreaks.com/topic/94661-making-a-code-recursive/#findComment-484731 Share on other sites More sharing options...
wildteen88 Posted March 6, 2008 Share Posted March 6, 2008 Change this line: ListFiles($dir); to: $arrFiles = ListFiles($dir); Link to comment https://forums.phpfreaks.com/topic/94661-making-a-code-recursive/#findComment-485189 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.