How to – Copy server’s default php.ini file to your account

PHP LogoWhen on a shared server, the need for a custom php.ini file comes up as one may need to make some changes with the configuration before the setup suits him best. But, because of the shared hosting environment, many hosts reject to offer the default php.ini file to make changes to.

One does have the option of creating a new php.ini file by placing only the required configuration changes in it but, it will revert all other configurations to php’s default. What it means is that it will not take the values for other configurations from server’s default file, instead it will revert them to php’s default values. So, it would be a lot better if one can access the default php.ini file to edit and use it in his account. Here’s how to do that:

  1. Log into your cPanel account or your FTP client and browse to the public_html directory.
  2. Create a file named getini.php here and paste the following script/code into it. The script accesses the default php.ini file, makes the required changes (if specified) and then writes the file to the defined directory.
    - - Start Script Here - -
    <?php
    // Put all the php.ini parameters you want to change below. One per line.
    // Follow the example format $parm[] = "parameter = value";
    $parm[] = "register_globals = Off";
    $parm[] = "session.use_trans_sid = 0";
    // full unix path - location of the default php.ini file at your host
    // you can determine the location of the default file using phpinfo()
    $defaultPath = "/usr/local/lib/php.ini";
    // full unix path - location where you want your custom php.ini file
    $customPath = "/home/USERNAME/public_html/php.ini";
    // nothing should change below this line.
    if (file_exists($defaultPath)) {
      $contents = file_get_contents($defaultPath);
      $contents .= "nn; USER MODIFIED PARAMETERS FOLLOWnn";
      foreach ($parm as $value) $contents .= $value . " n";
      if (file_put_contents($customPath,$contents)) {
        if (chmod($customPath,0600)) $message = "The php.ini file has been modified and copied";
          else $message = "Processing error - php.ini chmod failed";
      } else {
        $message = "Processing error - php.ini write failed";
      }
    } else {
      $message = "Processing error - php.ini file not found";
    }
    echo $message;
    ?>
    - - End Script Here - -
  3. Required Changes: Make the following changes to the above code after pasting:
    (i) – Change the $defaultPath value to the path of php.ini file on the server. Usually, the default value present in the code will work. If it didn’t work, you can find the path by using phpinfo()
    (ii) – Change the $customPath value to the path where you want to save the php.ini file fetched by the script.
    (iii) – Optionally, you can specify the changes you want to make in the fetched php.ini file using the format $parm[] = “parameter = value”; and the script will automatically make the changes before copying the file.
  4. Save the getini.php file and access it in a browser. If it was created in public_html directory, open http://YourDomain.com/getini.php and the script will automatically copy the php.ini file. If there’s any error in the operation, the script will display it.

Tested with PHP5, cPanel 11 and Apache. Leave a comment if you’re experiencing any problem.

Share This Post:
Share on FacebookTweet about this on TwitterShare on Google+Pin on PinterestShare on LinkedInShare on RedditEmail this to someone

,

2 Responses to How to – Copy server’s default php.ini file to your account

  1. Zarabianie July 20, 2013 at 11:17 am #

    Thanks, this is super helpful. First result in google and here’s the solution :-)

Leave a Reply