Php exec () and different hosts
I have a script that executes a file in several directories that look like this:
exec("php-cli $file_path > /dev/null 2>/dev/null &"); //php command-line
This works on most hosts, but they donβt like some hosts, and it does not start without any errors.
For hosts with which this fails, I use
exec("php $file_path > /dev/null 2>/dev/null &"); //notice the -cli is gone
Which works great.
$ file_path is the full path to the executable /home/blah/path/blah.php
How can I make this form work on all servers (at least unix)
** EDIT **
Well, I do it this way, this (most likely) is not the right way, but it works.
Instead of just using php-cli or php, I use both methods, so if one of them fails, the other goes through. Either php works like cgi, or cli, and one of them will catch, and since there is no way out, there would also be no error.
exec("php-cli $file_path > /dev/null 2>/dev/null &");
exec("php $file_path > /dev/null 2>/dev/null &");
, , . .
function php_exec($file_path) {
if (!($binary = which(array('php', 'php5', 'php-cli', 'php-cgi'))))
return false;
return exec("$binary $file_path > /dev/null 2>/dev/null &");
}
function which($binaries) {
if (!($path = getenv('PATH')) && !($path = getenv('Path')))
return false;
$arr = preg_split('/[:;]/', $path);
foreach ($arr as $p) {
foreach ($binaries as $b) {
if (file_exists("$p/$b"))
return "$p/$b";
}
}
return false;
}
var_dump(php_exec('test.php'));
: PHP php, php5, php-cli php-cgi. () ( * nix , PATH/Path) , , .
PATH: /bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin * nix (bash) C:\Windows\System32\;C:\Windows\; , preg_split ('/[:;]/')
, php_exec() false, php. , script.