I am wanting to call a php file using exec.
When I call it I want to be able to pass a variable through (an id).
I can call echo exec("php /var/www/unity/src/emailer.php");
fine, but the moment I add anything like echo exec("php /var/www/unity/src/emailer.php?id=123");
the exec call fails.
How can I do this?
To pass command line arguments to the script, we simply put them right after the script name like so... Note that the 0th argument is the name of the PHP script that is run. The rest of the array are the values passed in on the command line. The values are accessed via the $argv array.
The exec function is as safe as you make it. As long as you use the proper escaping functions like shown here, you'll be good. While it is possible to make the script commands safe, a common attack vector is to upload a malicious script and use exec and similar functions to hack the server.
If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function. Returns false on failure. To get the output of the executed command, be sure to set and use the output parameter.
php phpinfo(); ?> You can search for disable_functions and if exec is listed it means it is disabled. To enable it just remove the exec from the line and then you need to restart Apache and you will be good to go. If exec is not listed in the disable_functions line it means that it is enabled.
Your call is failing because you're using a web-style syntax (?parameter=value
) with a command-line invokation. I understand what you're thinking, but it simply doesn't work.
You'll want to use $argv
instead. See the PHP manual.
To see this in action, write this one-liner to a file:
<?php print_r($argv); ?>
Then invoke it from the command-line with arguments:
php -f /path/to/the/file.php firstparam secondparam
You'll see that $argv
contains the name of the script itself as element zero, followed by whatever other parameters you passed in.
try echo exec("php /var/www/unity/src/emailer.php 123");
in your script then read in the commandline parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With