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 - -
    // 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 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.

Get weekly updates via email!

Thank you for subscribing.

Something went wrong.

Do you like this post? Rate it:
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)


Click here to post a comment

Email me when somebody replies to my comment.

Get more stuff

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.