13

给定命令:

/usr/bin/php -c /path/to/custom/php.ini /path/to/script.php

我想获得内部选项:

-c /path/to/custom/php.ini

我试过的东西不起作用:

  • $argv包含['/path/to/script.php']
  • getopt('c')包含[]
  • $_ENV不包含它
  • $_SERVER 不包含它

我还寻找了一个PHP_*常数(例如PHP_BINARY),但找不到这些参数的常数。

有没有办法得到这些论点?请注意,我不是试图获取加载的ini文件,而是获取此处可能存在的任何参数。

4

4 回答 4

3

PHP 没有这样做的内部方法,因此您将不得不依赖某些系统信息和权限。

$pid = getmypid();
$ps = `ps aux | grep $pid`;
$command = substr($ps, strpos($ps, '/usr/bin/php'));
$args = explode(' ', $command); //not pretty, should probably use preg
于 2013-03-21T22:03:30.293 回答
2

-c /path/to/custom/php.ini is an option passed to the PHP parser, interpretator and other internall stuff before even starting your script.

/path/to/script.php is an actual argument passed not only to the PHP executable, but to your script.

Following arguments like /usr/bin/php -c /path/to/custom/php.ini /path/to/script.php A B C would also be passed to your script.

Unfortunately the -c option is not one of them.

You could get the used php.ini file within the executed PHP script by using get_cfg_var.

echo get_cfg_var('cfg_file_path');

If you are passing the -c option you would get the path to your php.ini file. Otherwise you would get the default php.ini file.

于 2013-03-21T21:47:49.153 回答
0

不幸的是,由于命令行 BASH 参数的解析方式,您将无法在脚本调用之前访问这些参数。目前,该程序/usr/bin/php

argv[0]=/usr/bin/php
argv[1]=-c
argv[2]=/path/to/custom/php.ini
argv[3]=/path/to/script.php

这就是你的论点落地的地方。另一方面,您的脚本具有:

argv[0]=/path/to/script.php

只是因为参数是从右到左处理的,并且在您的脚本调用之后没有参数。

于 2013-03-21T22:00:09.627 回答
0

我会使用如下的东西,因为它不需要任何解析。

$ (ARGS="-c /path/to/custom/php.ini"; /usr/bin/php $ARGS /path/to/script.php $ARGS)
于 2013-03-21T22:47:34.203 回答