10

I'm struggling to override the php cli configuration with my custom php.ini

My custom php.ini

max_execution_time = 90
memory_limit = 256M
safe_mode = Off

Running php cli

php -c /home/env/php.ini -r 'phpinfo();' | grep 'memory_limit'

outputs

memory_limit => 256M => 256M

However the custom php.ini doesn't seem to override max_execution_time nor safe_mode as it outputs 0 and On rather than 90 and Off.

Running this simple script

#!/usr/bin/php -c /home/env/php.ini
<?php echo 'memory_limit: ' . ini_get('memory_limit');

outputs the default cli configuration (128M) rather than 256M as expected.

Running this simple script

#!/usr/bin/php -d max_execution_time=90
<?php echo 'max_execution_time: ' . ini_get('max_execution_time');

outputs 90 as expected.

So far I've managed to override only one single configuration directive at runtime, so any help much appreciated how to override multiple configuration directives.

Edit:

php -a
php > parse_ini_file('/home/env/php.ini');

outputs no errors, so I guess my custom php.ini is ok. I'd though just discover that executing

#!/usr/bin/php -c /home/env/php.ini
<?php echo 'memory_limit: ' . ini_get('memory_limit'); phpinfo();

says Loaded Configuration File => (none). That seems to be the issue. PHP CLI doesn't seem to load my custom php.ini file even though the path is correct and syntax seems fine too.

Solution:

It doesn't seem possible to override multiple configuration directive at runtime with shebang, at least for me. But removing shebang and executing the following command solved the issue for me.

php -d max_execution_time=90 -d memory_limit=256M -d safe_mode=Off -f test.php
longtimejones
  • 121
  • 1
  • 7

2 Answers2

4

I was trying to set max_execution_time by CLI too and I was unable to do it.

From documentation I understand it is hard-coded and cannot be changed in CLI enviroment.

Anyway, I found the timeout command useful :

timeout 60 php -d memory_limit=1024M script.php

(run script for 60 seconds max).

adrianTNT
  • 3,671
  • 5
  • 29
  • 35
0

Try to put the line "safe_mode = Off" as the first line in your custom file. If I recall correctly, PHP parses the values in an ini file in order of occurrence.

That said, just disable safe_mode in every php.ini if you are allowed to do this on your server and don't rely on the feature anymore. After that, use the ini_set function in your code to change whatever you like, how many vars you like. I find it nicer to have the config that a scripts needs in the script itself rather than via extra commandline parameters or ini files.

Fyi, PHP safe mode is removed as from PHP version 5.4 so disabling it in older version is good practice to have less problems when upgrading to a newer version.

maartenh
  • 188
  • 11
  • Thanks for providing a possible solution. However I do not have access to that specific server no more. But your answer seems pretty reasonable. – longtimejones Sep 24 '14 at 15:41