B&T's Tips & Scripts
B&T's Tips & Scripts is a collection of Tips & Scripts to help you build a better website. The Tips & Scripts are targeted towards the native Apache PHP MySQL environment. Check back frequently as there will be additions and modifications. Please read our terms of use and our frequently asked questions. And while you are here, check out B&T's Tool Box.

Creating a custom php.ini file

Last updated: December 18, 2007

You may want to use a custom php.ini file in order to change some of the host defaults.  If you copy the default host file, make changes, and then put it in your webspace, you may not know if your host makes further changes to the default file which could cause your file to be out-of-date.  A good solution is the following script, which you can execute manually or kick-off with cron each night.  The script reads the host default file, makes your changes, and writes it to your directory.  Now you can always have the current host default php.ini file with your changes made.

Note 1 - When making a custom php.ini file you must include all the parameters.  If you just make an abreviated file of just what you want to change then all other parameters will revert to the php defaults.  This means you would not have other changes that have been made by your host.  Just one more reason it is good to use this script to customize, starting with the current host file.

Note 2 - You may have to change the file/directory paths in the examples below to match your hosting environment.

You can download this script as a .txt file.  Remember to rename the file as a .php file.

- - 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/user/public_html/php.ini";
// nothing should change below this line.
if (file_exists($defaultPath)) {
$contents file_get_contents($defaultPath); 
$contents .= "\n\n; USER MODIFIED PARAMETERS FOLLOW\n\n";  
  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";
$message "Processing error - php.ini chmod failed";
  } else {
$message "Processing error - php.ini write failed";
} else {
$message "Processing error - php.ini file not found";

- - End Script Here - -

You may want to change other things in php.ini as well.  You can do this by repeating the $parm[] statement for as many changes as you wish to make.

For example you may want to use your own temp directory for session files.  To do this add the following line:
$parm[] = "session.save_path = /home/user/temp";   // user specified temp session file directory
Note the sessions directory is above the public directory for added security.

You may want to use your own temp upload directory for improved security.  To do this add the following line:
$parm[] = "upload_tmp_dir = /home/user/temp";   // user specified temp upload directory
Note the uploads directory is above the public directory for added security.

If you want to change the maximum file upload size to something larger than 2MB, add the following:
$parm[] = "upload_max_filesize = 4M";   // user specified max file upload size

And if you want to go larger than 8MB, also add the folowing:
$parm[] = "post_max_size = 10M";   // user specified post max size
The above script does a chmod 600 on the custom php.ini file which should adequately protect it from prying eyes.
An alterative to using a custom php.ini file is to use the php function ini_set() in your scripts to modify the default php parameter values.  This is probably the best solution for custom scripts, but not practical if you use applications which you do not know how to modify.

This function is documented here.  Note that you can only change the php parameters listed as Changeable - PHP_INI_ALL (as shown here) using this method.
Rate This Topic
topic rating using this script.
You are visitor 1,538,895 (using this script)
Copyright © 2004-2014