Cannot run shell (which) command in PHP code

老子叫甜甜 提交于 2019-12-24 08:52:46

问题


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:


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



来源:https://stackoverflow.com/questions/50210164/cannot-run-shell-which-command-in-php-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!