php.ini fix

In some web server environments, you can use a small php.ini file amongst your site files to make a few changes to the server's main php.ini file. For example you might want to allow file uploads up to 10 megabytes by creating a php.ini containing:

upload_max_filesize = 10M
post_max_size = 10M

However on some servers, the presence of this file causes PHP to only read this file, and not the server's main php.ini file at all. The usual way to get around this problem is to take a copy of the main php.ini, and make the changes required for your site. The following script makes it easy to do this, and can be run any time your host changes the main php.ini. It could even be set up as a cron job and run regularly, so you will always have an up to date php.ini file.

// php-ini-fix.php

// 1. Put the php.ini parameters you want to change
// between the two INI_SETTINGS lines below
$newSettings = <<<INI_SETTINGS

upload_max_filesize = 10M
post_max_size = 10M


// 2. Set the location of the default php.ini file.
// You can find the correct location using phpinfo()
$defaultFile = '/usr/local/lib/php.ini';

// 3. Name of the custom php.ini file to create
$createFile = './php.ini';

// do not edit below here
if (file_exists($defaultFile))
// cut-down version for php4 - replaces the contents of $filename with $string, returns true or false
function file_put_contents($filename, $string) { if (!$h = @fopen($filename, 'w')) return false; if (!fwrite($h, $string)) return false; fclose($h); return true; }
$contents.="\n\n; CUSTOM PARAMETERS\n; -----------------\n\n";
if (
file_put_contents($createFile, $contents))
$message='Your php.ini file has been created.'; }
$message='Unable to write php.ini file.'; }
else {
$message='Default php.ini file not found.'; }


With some server configurations, a php.ini file in your root directory (public_html) will apply to PHP scripts at that level and all subdirectories. However with servers running a security module such as suExec, you will usually need a separate php.ini in each directory where it is required. To avoid this requirement and only have one php.ini file that applies to all directories, you can add this line to your .htaccess file:

SetEnv PHPRC /home/myusername/public_html/subdir

then the php.ini in that directory will apply to your whole site.

Write a comment

  • Required fields are marked with *.