Reputation: 45
I'm trying to execute a separate PHP script from within a PHP page. After some research, I found that it is possible using the exec()
function.
I also referenced this SO solution to find the path of the php binary. So my full command looks like this:
$file_path = '192.168.1.13:8080/doSomething.php';
$cmd = PHP_BINDIR.'/php '.$file_path; // PHP_BINDIR prints /usr/local/bin
exec($cmd, $op, $er);
echo $er; // prints 127 which turns out to be invalid path/typo
echo "Hi there!";
I know $file_path
is a correct path because if I open its value; i.e. 192.168.1.13:8080/doSomething.php
, I do get "Hi there!" printed out. This makes me assume that PHP_BINDIR.'/php'
is wrong.
Should I be trying to get the path of the php binary in some other way?
Upvotes: 0
Views: 438
Reputation: 12035
The file you are requesting is accessible via a web server, not as a local PHP script. Thus you can get the result of the script simply by
$output = file_get_contents($file_path);
If you however for some reason really have to exec
the file, then you must provide a full path to that file in your server directory structure instead of server URL:
$file_path = '/full/path/to/doSomething.php';
$cmd = PHP_BINDIR.'/php '.$file_path;
exec($cmd, $op, $er);
Upvotes: 1