开发者

Find all .htaccess files and replace content with PHP script

I need to create a script that will find all my .htaccess files on my server and replace their content with the new content I need in order for my pages to be SEO friendly.

So far I've come across a few scripts that will find all the .htaccess files but I need to be able to open, replace all content with new and save with the proper permissions.

Can anyone help me with the following code to add the extra functionality I need?

<?php

function searchDir($dir) {
   $dhandle = opendir($dir);
   if ($dhandle) {
      // loop through it
      while (false !== ($fname = readdir($dhandle))) {
         // if the element is a directory, and does not start with a '.' or '..'
         // we call searchDir function recursively passing this element as a parameter
         if (is_dir( "{$dir}/{$fname}" )) {
            if (($fname != '.') && ($fname != '..')) {
               echo "Searching Files in the Directory: {$dir}/{$fname} <br />";
               searchDir("$dir/$fname");
            }
         // if the element is an .htaccess file then replace content
         } else {
            if($fname == ".htaccess")
            {
               echo "Replacing content of file ".$dir/$fname."<br />";
               // I need the code for editing the files here.
  开发者_Go百科          }
         }
      }
      closedir($dhandle);
    }
}

searchDir(".");


?>


Modify your else loop with this

else {
  if($fname == ".htaccess")
  {
   echo "Replacing content of file ".$dir/$fname."<br />";
   // I need the code for editing the files here.
   $htaccess_content = " Your htaccess string " ;  // you can do this assignment at the top
   file_put_contents("{$dir}/{$fname}",$htaccess_content) ;
  }
}


Use file_get_contents() and file_put_contents().

http://php.net/manual/en/function.file-get-contents.php
http://php.net/manual/en/function.file-put-contents.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜