Thursday, March 12, 2009

how to customize your php settings for a folder

Lets say i have a particular file in php that processes file uploads and takes executes a program which takes about 4 minutes for full execution. By default, the php configuration is set to maximum file size of 2MB and execution time of about 60 seconds(1 minute). If you have to change the configuration in the php.ini file, it will change the settings for all the projects that are present in the server. This is not advisable. Again if you are trying to host them on  a server, the hosting party might not allow you to do so. Here are few work arounds that I would suggest.

1) ini_set() function PHP allows the configuration values to be set within a file itself for particular configuration parameters.  This function can be used in any file which you want the customized configuration. Further this is limited to only certain configuration parameters.  This will be discussed in the later part.

eg: ini_set('display_errors',1);

2) php.ini : PHP allows a customised php.ini file to be located in a single directory to override the parameters that are present in the root directory. Many of the hosting providers support this type of functionality. Only the parameters that you might want to be changed can be written in this file and stored in the directory to which you want to apply. However some hosting providers wont let you change some parameters like max execution time for their performance purpose.

eg:

display_errors 1
upload_max_filesize =13M
post_max_size= 20M

Thus your problem gets solved

3).htaccess:  For those who can tweak the .htaccess file to a value, this is one more option. You can create a .htaccess file for the directory. Note that in the main httpd.conf file, you have 

allowOverride Options

for the directory. Else this will not work. In the .htaccess file, you can specify the values for some parameters and also flag them (for magic_quotes, display_errors which have only two values)
Again this is to be tweaked and kept properly.

Eg :

php_value upload_max_filesize 13M
php_value post_max_size 20M

Please note that if you try to use it with .htaccess, you will have to restart the server to make it come to effect.


Now here is an explanation of what can be changed and what params cannot be changed in the php configuration and their levels.






In the appendix of the above link, the Changeable  column decides where the functions can be changed. 

PHP_INI_PERDIR implies that the settings needed to be changed in a directory level and not in a single file. This can be done either using method 2 or 3 that i mentioned above. The minimum php versions are specified in the link.

PHP_INI_ALL implies that the param settings can be changed anywhere. you can any of the methods that i specified.

PHP_INI_SYSTEM implies that the param settings can be changed only in the root php.ini file and cannot be changed else where.

I have struggled about three days to get to know about this and didnt want to make it lost.