Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling exec on a php file and passing parameters?

Tags:

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?

like image 671
Hailwood Avatar asked Jun 08 '11 03:06

Hailwood


People also ask

How do I pass a command line argument in PHP?

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.

Is it safe to use exec in PHP?

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.

How do I execute a PHP command?

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.

How do you check if exec is enabled in PHP?

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.


2 Answers

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.

like image 66
Chris Allen Lane Avatar answered Nov 12 '22 09:11

Chris Allen Lane


try echo exec("php /var/www/unity/src/emailer.php 123"); in your script then read in the commandline parameters.

like image 40
hakre Avatar answered Nov 12 '22 09:11

hakre