PHP Path for shell_exec() to call PHP file
Solution 1
If so, how can I find the php path and specify it in my environment? Also does that mean that if I distribute a script which uses shell_exec()...
Unfortunately, yes. The environment where you distribute the script must have a command line PHP (which is not necessarily the same as the PHP used by the Web server - it might be a different version).
And there is no easy way of "finding" it.
So what you would need to do is, you clearly state (when distributing) that PHP command line executable must be in the PATH.
At that point, shell_exec('php -q -f yourfile...') ought to work.
You can redirect the output to a file (always the same if you want) and delete it if there are no errors. Otherwise the file will show what errors occurred, which may be useful.
UPDATE: and the first times, to debug, don't redirect the output, but try:
<?php
Header("Content-Type: text/plain");
error_reporting(E_ALL);
print shell_exec('php "' . __DIR__ . '/log.php" 2>&1'); // > /dev/null 2>/dev/null &');
?>
This way you can see if the command hits some other snag, such as not having the rights to create the log.txt file in the first place.
Solution 2
If your files are in a directory tree containing a space, you have a problem:
shell_exec('php ' . __DIR__ . '/log.php > /dev/null 2>/dev/null &');
becomes:
shell_exec('php somepath/with spaces in it/log.php > /dev/null 2>/dev/null &');
which makes php run somepath/with
as a script with the arguments:
'spaces', 'in', 'it/log.php'
try putting quotes around your argument:
shell_exec('php "' . __DIR__ . '/log.php" > /dev/null 2>/dev/null &');
Teno
My account seems to be hacked by some of the moderators. This profile also got changed by somebody a few times. I migrated to Quora, an awesome Q & A web site. I feel very comfortable there.
Updated on June 23, 2022Comments
-
Teno almost 2 years
I'm trying to use
shell_exec()
and having hard time understanding how to use it. I referred to this page: Is there a way to use shell_exec without waiting for the command to complete?.I created two PHP files:
shell_exec.php
<?php shell_exec('php ' . __DIR__ . '/log.php > /dev/null 2>/dev/null &'); ?>
log.php
<?php $file = __DIR__ . '/log.txt'; $current = time() . ": sample text\r\n"; file_put_contents($file, $current,FILE_APPEND); ?>
Then I accessed the page of shell_exec.php from my browser. I expected that it runs log.php and creates a file. However, nothing happens.
I suspect that the php paths specified in the function, which is
php
in the above code, is different in different operating system. I'm currently testing in Windows, so that may be the reason that it doesn't work. But I'm not 100% sure.If so, how can I find the php path and specify it in my environment? Also does that mean that if I distribute a script which uses
shell_exec()
and call a php script, do I have to write some code to find out the php path? In that case, is there a good way of doing it? -
LSerni over 11 yearsHe did, but '-f' is optional anyway.
-
Teno over 11 yearsI got
'php' is not recognized as an internal or external command, operable program or batch file.
-
Teno over 11 yearsIndeed there was a space in the directory path. And I tried to put the double colons as you suggest but still not working. Thanks for the answer.
-
Teno over 11 yearsI need to run a background process. That's why.
-
LSerni over 11 yearsEuh. Then it means it is not in the system PATH (or maybe it's not even installed: having Webserver PHP doesn't imply having Commandline PHP). Check you do have the PHP binary, and then add the appropriate path to your system configuration.
-
Teno over 11 yearsFinally
shell_exec('Z:\xampp\php\php.exe "' . __DIR__ . '/log.php" 2>&1');
worked. Thanks. But is it possible to find the path programmatically? -
Teno over 11 yearsAs I read the link dbf posted, it seems to be available since PHP v5.4.
-
LSerni over 11 yearsIt is not (easily) possible, because the "sure way" would be to search the whole disk with
opendir()
andfile_exists()
. If it is in the system PATH it works automatically, if it is not you can't know where it is. Say that you installed XAMPP and also Wamp-X and also the PHP MSI package: you get three different PHP.EXE -- which is the one you want? There is no function that can tell you that, and ay, there's the rub.