The above example function called return_bytes() assumes that ini_get('upload_max_filesize') delivers only one letter at the end. As I've seen 'Mb' and things like that, I'd suggest to change the $last = ... part into $last = strtolower(substr($val,strlen($val/1),1)).
I'd call it $unit then.
ini_get
(PHP 4, PHP 5)
ini_get — 設定オプションの値を得る
説明
string ini_get
( string $varname
)
成功時に、設定オプションの値を返します。
パラメータ
- varname
-
設定オプションの名前。
返り値
成功した場合に設定オプションの値、失敗した場合あるいは null 値を指定した場合に空の文字列を返します。
例
例1 ini_get() の例
<?php
/*
php.ini で以下のように設定されているものとします
display_errors = On
register_globals = Off
post_max_size = 8M
*/
echo 'display_errors = ' . ini_get('display_errors') . "\n";
echo 'register_globals = ' . ini_get('register_globals') . "\n";
echo 'post_max_size = ' . ini_get('post_max_size') . "\n";
echo 'post_max_size+1 = ' . (ini_get('post_max_size')+1) . "\n";
echo 'post_max_size in bytes = ' . return_bytes(ini_get('post_max_size'));
function return_bytes($val) {
$val = trim($val);
$last = strtolower($val[strlen($val)-1]);
switch($last) {
// 'G' は PHP 5.1.0 以降で使用可能です
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}
?>
上の例の出力は、たとえば 以下のようになります。
display_errors = 1 register_globals = 0 post_max_size = 8M post_max_size+1 = 9 post_max_size in bytes = 8388608
注意
注意: boolean 値を探す場合
off という boolean の ini 値は空文字列または "0" として返されます。 一方で on の ini 値は "1" として返されます。 また、この関数はリテラル文字列で設定された INI 値を返すこともできます。
注意: メモリサイズの値を探す場合
upload_max_filesize のようなメモリサイズの値の場合、 php.ini上で省略形で格納されています。 ini_get()はphp.iniに格納されている値を そのままの形式で返します。整数表現に変換したりはしません。 これらの値に通常の算術的な関数を施すと予期しない結果を 得てしまいます。上の例では、省略形の表記から本来のバイト数に変換する PHP ソースのひとつの例を示しています。
ini_get
peter
29-Jul-2008 05:25
29-Jul-2008 05:25
filh at filh dot org
21-Nov-2005 09:24
21-Nov-2005 09:24
Concerning the value retourned, it depends on how you set it.
I had the problem with horde-3 which test the safe_mode value.
THan :
- if you set the value with php_admin_value safe_mode Off (or On) ini_get returns the string
- if you set the value with php_admin_flag safe_mode Off (or On) ini_get returns the boolean.
01-Nov-2005 02:16
Important: The manual says that ini_get will return 0 or an empty string for boolean config values that are set to off in php.ini.
This is technically correct, however when you use
php_value register_globals off
in an .htaccess file, ini_get will return the string, which will "evaluate" to 1. So if you are using mod_php you have to check boolean config values against the strings (upper/lowercase etc.) anyhow or you will get wrong results.
david dot tulloh at infaze dot com dot au
21-Jun-2005 10:01
21-Jun-2005 10:01
You can set custom entries in the ini file to provide globals such as database details.
However these must be retrieved with get_cfg_var, ini_get won't work.
Der Graph
16-Aug-2004 02:59
16-Aug-2004 02:59
It might be useful for included scripts that include other files to extend the 'include_path' variable:
<?php ini_set('include_path',ini_get('include_path').':../includes:'); ?>
Sometimes, it may also be useful to store the current 'include_path' in a variable, overwrite it, include, and then restore the old 'include_path'.
fbeyer at <nospam>clickhand dot de
13-Aug-2002 10:29
13-Aug-2002 10:29
If you want to test ini flags (eg. On/Off), I recommend to explicitly cast the value returned by ini_get() to boolean - it is cleaner as you only get true or false, not 0 or 1 or "" as described above.
<?php
$register_globals = (bool) ini_get('register_gobals');
?>
C fans may of course also cast it to (int) to play with 0 and 1 - that's also cleaner to print().
