Levi Morrison
Levi Morrison

Reputation: 19552

PHP: How to get internal arguments?

Given the command:

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

I'd like to get the internal options:

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

Things I've tried that do not work:

I've also looked for a PHP_* constant (such as PHP_BINARY) but cannot find one for these arguments.

Is there any way to get these arguments? Note that I am not trying to obtain the loaded ini file but any arguments that might be present here.

Upvotes: 13

Views: 308

Answers (4)

Alexander
Alexander

Reputation: 23537

I'd use something as follows since it doesn't require any parsing.

$ (ARGS="-c /path/to/custom/php.ini"; /usr/bin/php $ARGS /path/to/script.php $ARGS)

Upvotes: 0

Harry Dobrev
Harry Dobrev

Reputation: 7706

-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.

Upvotes: 2

bizzehdee
bizzehdee

Reputation: 21003

PHP has no internal way of doing this, so you are going to have to rely on certain system information and permissions.

$pid = getmypid();
$ps = `ps aux | grep $pid`;
$command = substr($ps, strpos($ps, '/usr/bin/php'));
$args = explode(' ', $command); //not pretty, should probably use preg

Upvotes: 3

TopherGopher
TopherGopher

Reputation: 673

Unfortunately, because of how command line BASH arguments are parsed, you won't be able to access the arguments before your script call. Right now, the program /usr/bin/php has

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

And that's where your arguments are landing. Your script on the other hand has:

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

Just because the arguments are processed right to left, and there are no arguments after your script call.

Upvotes: 0

Related Questions