无法在PHP代码中运行shell命令 [英] Cannot run shell (which) command in PHP code

查看:66
本文介绍了无法在PHP代码中运行shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用Flyway,并且使用PHP exec()函数运行特定的命令:

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

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

只要我指定了绝对路径,这些命令就可以使用,但是可能会有所不同,具体取决于它将使用的计算机.这就是为什么我想使用通过命令exec('which flyway')确定绝对路径的变量.

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').

问题是,即使我直接在shell中编写它,它也会返回一个空值,从而获得所需的结果.我还尝试使用php交互式外壳php -a,如果我运行命令echo exec('which flyway'),它还会返回所需的路径,当我直接在代码中直接编写它时,会得到NULL结果.

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.

请注意,如果我想验证php(which php)的绝对路径,则可以在外壳程序php -a或我的代码中执行此操作,并且在所有三种情况下均返回所需的结果.因此,which flyway命令是唯一在我的代码中结果为空的命令.

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 ?

推荐答案

which使用与在没有绝对路径的情况下运行命令时使用的路径变量相同的路径变量.因此,您可以直接使用它们(因为它们在路径上)并且根本不需要which,或者which根本找不到可执行文件.

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.

此外,请记住,并非所有托管程序都允许在PHP进程中任意运行.您不应该在这样的基础上构建应用程序

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

这篇关于无法在PHP代码中运行shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆