php exec 函数中的非法命令错误代码 127

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/438618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:45:04  来源:igfitidea点击:

illegal command error code 127 in php exec function

phpexec

提问by Intellex

I am using this php code:

我正在使用这个 php 代码:

exec("unrar e file.rar",$ret,$code);

and getting an error code of illegal command ie 127 ... but when I am using this command through ssh its working ... because unrar is installed on the server ... so can anyone guess why exec is not doing the right stuff?

并得到一个非法命令的错误代码,即 127 ...但是当我通过 ssh 使用这个命令时它的工作......因为服务器上安装了 unrar......所以有人能猜到为什么 exec 没有做正确的事情吗?

回答by AAA

Try using the direct path of the application (/usr/bin/unrar of whatever), it sounds like php can't find the application.

尝试使用应用程序的直接路径(/usr/bin/unrar 什么的),这听起来像 php 找不到应用程序。

回答by Rogelio Trivi?o

If you have chrooted apache and php, you will also want to put /bin/sh into the chrooted environment. Otherwise, the exec() or passthru() will not function properly, and will produce error code 127, file not found.

如果你有 chrooted apache 和 php,你还想把 /bin/sh 放到 chrooted 环境中。否则,exec() 或 passthru() 将无法正常运行,并会产生错误代码 127,找不到文件。

回答by Brandon

Since this comes up as a top answer in google, I wanted to share my fix:

由于这是谷歌的最佳答案,我想分享我的修复:

The simple fix I had was to disable safe_mode in the php.ini file

我的简单修复是在 php.ini 文件中禁用 safe_mode

; Safe Mode
; http://www.php.net/manual/en/ini.sect.safe-mode.php#ini.safe-mode
safe_mode = Off

回答by Intellex

thanx all for your response!!

感谢您的回复!!

I tried this

我试过这个

//somedir is inside the directory where php file is
chdir("somedir");
exec("/home/username/bin/unrar e /home/path/to/dir/file.rar");

and now it returned no exit code ... oher commands are doing file .. i tried mkdir etc .. :s

现在它没有返回退出代码......其他命令正在执行文件......我试过mkdir等......:s

回答by ChrisH

Just in case somebody else still gets this problem, take a look at the post here:

以防万一其他人仍然遇到此问题,请查看此处的帖子:

http://gallery.menalto.com/node/2639#comment-8638

http://gallery.menalto.com/node/2639#comment-8638

Quote:

引用:

I found the problem. The problem was my security-paranoid OpenBSD. When upgrading from 3.1 to 3.2 they added:

  • Apache runs chroot'd by default. To disable this, see the new -u option.

The chroot prevented Apache from accessing anything outside of a directory, so I moved everything into the apache directory including netpbm. Everything was accessible and executable, but I guess it was still in some sort of "safe mode" because the exec() always returned 127.

Anyway, running httpd with the -u option went back to the less secure non chroot'd apache startup, which allowed the exec() to work again.

我发现了问题。问题是我的安全偏执型 OpenBSD。从 3.1 升级到 3.2 时,他们添加了:

  • Apache 默认运行 chroot。要禁用此功能,请参阅新的 -u 选项。

chroot 阻止 Apache 访问目录外的任何内容,因此我将所有内容都移到了 apache 目录中,包括 netpbm。一切都是可访问和可执行的,但我想它仍然处于某种“安全模式”,因为 exec() 总是返回 127。

无论如何,使用 -u 选项运行 httpd 回到了安全性较低的非 chroot 的 apache 启动,这允许 exec() 再次工作。

回答by Intellex

ohkiee guyz thanx ... and yes there might be some errors with $PATH ... but with given full path its working :)

ohkiee Guyz thanx ......是的,$PATH 可能存在一些错误......但是给定的完整路径可以正常工作:)

exec("/home/user/bin/unrar e /home/user/xxx/yyy/file.rar");