How to copy php.ini file to all folders

Post Reply
Tony
Lieutenant
Lieutenant
Posts: 86
Joined: Tue Jul 21, 2009 4:11 pm

How to copy php.ini file to all folders

Post by Tony » Sun Nov 29, 2009 4:27 am

You can find another Tip on this page for how to create a custom php.ini file. But after you create it, many hosts will require you to put a copy in each directory. This script makes it easy. It will copy the php.ini file from your main public directory and put one in all the subdirectories. If you do not want it in every subdirectory, after you delete the ones you do not want you can do all future updates with the overwriteOnly flag on.

Code: Select all

<?php
// set this value to Y if you only want to overwrite old php.ini files
// set this value to N if you want to put a php.ini file in every directory
$overwriteOnly = "Y";
// full path to the location of your home directory where the php.ini file is located that you want to copy to lower lever directories
$path = "/home/" . get_current_user() . "/public_html";
// change nothing below this line
if ($overwriteOnly == "Y") echo "Operating in Overwrite Only Mode<br><br>";
$source = $path . "/php.ini";
if (!file_exists($source)) die('Error - no source php.ini file');
function search($dir) {
  global $source, $overwriteOnly;
  foreach(scandir($dir) as $filename) {
    if ($filename !== '.' AND $filename !== '..' AND $filename !== 'cgi-bin' AND is_dir("$dir/$filename") ) {
      $path = $dir."/".$filename; 
      $target = $path . "/php.ini";
      if (!file_exists($target) AND $overwriteOnly == "Y") {
        echo "$path <b>skipped - no php.ini file</b><br>";
      } else {
        echo "$target <br>";
        if (!copy($source,$target)) echo "<b>Write failed for $target </b><br>";
        if (file_exists($target)) chmod($target,0600);
    }
      search($path);
    }
  }
}
search($path);
echo "<br>Done.";
?>
Post Reply

Return to “PHP & MySQL”