Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i check if ini_set will work on the server?

How can i check if server configuration allows me to set an option like:

ini_set('upload_max_filesize', '8M');

in a PHP script? Here is a list of php.ini directives but i can't really figure out how to make a check before tring to change that value.

like image 796
Polmonino Avatar asked Dec 17 '11 10:12

Polmonino


2 Answers

Check if I'm allowed to use ini_set for some option, how?

ini_set will return the old value on success, and false* on failure. With this knowledge you can write a statement checking if your call went through, like the below.

$result = ini_set ("some_option", "some_value");
$failure = (version_compare(PHP_VERSION, '5.3.0') >= 0) ? false : '';
if ($result === $failure)
   echo "Unable to use ini_set to change 'some_option'!";

(*): Note the return value changed in PHP 5.3.0 from '' (an empty string) to false. So you need to check your current PHP version as well.


Another method is to use ini_get_all which will provide you with details regarding every option available, and it's access level.

$all_option_details = ini_get_all ();

/* see the comments in this post regarding PHP_INI_USER vs INI_USER
 * seems like someone writing the relevant PHP documentation fcuked up
 *
 * props to @soulmerge */

if ($all_option_details['upload_max_filesize']['access'] & INI_USER)
   echo "we are allowed to change upload_max_filesize from with ini_set!";

I'd like to disable the use of ini_set for some option(s), how?

There are a few methods of making options unchangeable runtime (in a way disabling ini_set), among them are the following two which you can read more about at the provided link.

  • PHP: How to change configuration settings

php_admin_value name value

Sets the value of the specified directive. This can not be used in .htaccess files. Any directive type set with php_admin_value can not be overridden by .htaccess or ini_set(). To clear a previously set value use none as the value.

 

php_admin_flag name on|off

Used to set a boolean configuration directive. This can not be used in .htaccess files. Any directive type set with php_admin_flag can not be overridden by .htaccess or ini_set().


Example (taken from this documentation)

<IfModule mod_php5.c>
  php_value include_path ".:/usr/local/lib/php"
  php_admin_flag engine on
</IfModule>

<IfModule mod_php4.c>
  php_value include_path ".:/usr/local/lib/php"
  php_admin_flag engine on
</IfModule>
like image 92
Filip Roséen - refp Avatar answered Sep 28 '22 02:09

Filip Roséen - refp


It is impossible to know what your real limit is unless you try to set the desired value. There might be the suhosin patch installed, for example, which could prevent you from changing the value at all.

So your only option for checking if it is possible is to try it and check the return value (which you should to in any case):

$oldValue = ini_get('upload_max_filesize');
if (ini_set('upload_max_filesize', '8M') === false) {
    die("Couldn't update upload file size.");
}
if (ini_set('upload_max_filesize', $oldValue) === false) {
    die("Error resetting upload file size.");
}
// you can safely assume that it is possible to set
// upload_max_filesize to 8M from this line onward.
like image 33
soulmerge Avatar answered Sep 28 '22 02:09

soulmerge