Jump to content

making a code recursive


blueman378

Recommended Posts

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

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;
    }
}

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

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

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

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.