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 Quote Link to comment 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; } } Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.