Page 1 of 1

How to delete php.ini files using PHP

Posted: Sun Nov 29, 2009 4:28 am
by Tony
You can find another Tip on this page for how to create a custom php.ini file. And another for how to populate directories with your php.ini file in the event your host requires you to have one in each directory with php scripts. But then if you want to delete all the php.ini files, you may need this script. It will delete any php.ini file from your main public directory and all subdirectories.

Code: Select all

<?php
// this script will delete all your php.ini files
// full path to the location of your home directory
$path = "/home/" . get_current_user() . "/public_html";
// change nothing below this line
function search($dir) {
  foreach(scandir($dir) as $filename) {
    if ( $filename !== '.' AND $filename !== '..' AND is_dir("$dir/$filename") ) {
      $path = $dir."/".$filename; 
      $target = $path . "/php.ini";
      if (file_exists($target)) {
        if (unlink($target)) echo "Deleted - $target <br>"; else echo "<b>Delete failed for $target </b><br>";
      }
      search($path);
    }
  }
}
$target = $path . "/php.ini";
if (file_exists($target)) {
  echo "Deleting - $target <br>";
  if (!unlink($target)) echo "<b>Delete failed for $target </b><br>";
}
search($path);
echo "<br>Done.";
?>