Cannot run shell (which) command in PHP code

2019-07-27 10:02发布

So I am working with Flyway and I run specific commands using PHP exec() function:

exec('/path/absolute/flyway info');

These commands work as long as I specify the absolute path, but that may vary depending on the machines that it will be working on. That is why I want to use a variable which determines that absolute path, through the command exec('which flyway').

The thing is that this returns a null value, even though when I write it directly in shell I get the desired result. I also tried using the php interactive shell php -a, where if I run the command echo exec('which flyway') it also returns the desired path, altough when I write it directly in my code, I get the NULL result.

Note that if I want to verify the absolute path of php (which php), I can do that in the shell, php -a or in my code, and it returns the desired result in all three cases. So the which flyway command is the only one that has a null result in my code.

Can anyone please help me in this matter ?

1条回答
Luminary・发光体
2楼-- · 2019-07-27 10:10

which uses the same path variables which are used when you run the command without the absolute path. So, either you could use them directly (because they are on the path) and would not need which after all, or which is not able to find the executables after all.

Additionally, keep in mind that not all hosters allow to run arbitrary from a PHP process. You should not built your application on such a foundation

查看更多
登录 后发表回答