milenmk
milenmk

Reputation: 544

Execute php script with arguments from another php file

I'm playing with a script that I had to call from another php file. From file update.php, at the beginning I have to start the script by calling its file serv.php. But, in order for serv.php to work I had to pass a couple of arguments. The command to start serv.php from command line is as follow:

php c:\path_to_folder\serv.php run --argument1="some_text" --argument2="some_text" --port=some_port

If we presume that update.php and serv.php are at the same folder, how to call serv.php from within update.php?

Upvotes: 1

Views: 7767

Answers (4)

Razorphyn
Razorphyn

Reputation: 1332

shell_exec("php c:\path_to_folder\serv.php some_text some_text some_port ");

usually is php -f or php5-cli instead of php and some servers don't even require the path to the php folder
This is one of my query:

$ex=$setting[8]." ".dirname(__FILE__)."/sendmail.php NewMem ".$_SESSION['id']." ";
if(substr(php_uname(), 0, 7) == "Windows")
    pclose(popen("start /B ".$ex,"r")); /windows server
else
    shell_exec($ex." > /dev/null 2>/dev/null &");//Apache

where $setting[8] is php -f or php5-cli, ".dirname(__FILE__)."/sendmail.php is the path to the file, and the others are the arguments .
To retrieve the passed arguments use $argv that contains all the other parameter $argv[0] is the file path.
Note that each space define a new argument

Upvotes: 0

mavili
mavili

Reputation: 3424

You can execute a system command using the exec function. PHP exec().

You can call your page serv.php from your update.php script as such

exec('php c:\path_to_folder\serv.php run --argument1="some_text" --argument2="some_text" --port=some_port');

Upvotes: 0

Sumit Gupta
Sumit Gupta

Reputation: 2192

use exec or system call within your calling PHP Script. do mention full path of PHP CLI i.e. /user/bin/php or whatever path is there in your server.

Upvotes: 0

Ilya
Ilya

Reputation: 4689

Send $argv of serv.php from serv.php to update.php (see http://php.net/manual/en/reserved.variables.argv.php). I.e. in update.php you will have another $argv, so you have to send list of command line parameters in array with different name.

Upvotes: 1

Related Questions