简体   繁体   English

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

[英]illegal command error code 127 in php exec function

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 没有做正确的事情吗?

尝试使用应用程序的直接路径(/ usr / bin / unrar的任何东西),听起来像php无法找到应用程序。

If you have chrooted apache and php, you will also want to put /bin/sh into the chrooted environment. 如果您已经chrooted apache和php,您还需要将/ bin / sh放入chrooted环境中。 Otherwise, the exec() or passthru() will not function properly, and will produce error code 127, file not found. 否则,exec()或passthru()将无法正常运行,并将生成错误代码127,找不到文件。

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

thanx all for your response!! 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

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. 问题是我的安全偏执的OpenBSD。 When upgrading from 3.1 to 3.2 they added: 当从3.1升级到3.2时,他们补充说:

  • Apache runs chroot'd by default. Apache默认运行chroot。 To disable this, see the new -u option. 要禁用此功能,请参阅新的-u选项。

The chroot prevented Apache from accessing anything outside of a directory, so I moved everything into the apache directory including netpbm. chroot阻止Apache访问目录之外的任何内容,因此我将所有内容都移到了apache目录中,包括netpbm。 Everything was accessible and executable, but I guess it was still in some sort of "safe mode" because the exec() always returned 127. 一切都是可访问和可执行的,但我想它仍然处于某种“安全模式”,因为exec()总是返回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. 无论如何,使用-u选项运行httpd会返回到安全性较低的非chroot'd apache启动,这使得exec()可以再次运行。

ohkiee guyz thanx ...是的,$ PATH可能会有一些错误...但是给定完整路径它的工作:)


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

I did not find the solution for my problem of the same type so sharing what was the cause of it in my Linux setup.我没有找到相同类型问题的解决方案,因此在我的 Linux 设置中分享了它的原因。 For whatever reason I needed an apache module loaded before other modules and so apache was started with LD_PRELOAD.无论出于何种原因,我需要在其他模块之前加载 apache 模块,因此 apache 以 LD_PRELOAD 启动。 As exec inherits the environment of the parent process LD_PRELOAD was used for starting the exec-ed program (through sh).由于 exec 继承了父进程的环境 LD_PRELOAD 用于启动 exec 程序(通过 sh)。 Now the preloaded module uses some bindings to apache functions and of course they are not to be present in sh.现在预加载的模块使用了一些与 apache 函数的绑定,当然它们不会出现在 sh 中。 The result of the php exec was an exit status of 127. The solution was to have in my php file a putenv("LD_PRELOAD") that gets executed before any exec calls. php exec 的结果是退出状态 127。解决方案是在我的 php 文件中有一个 putenv("LD_PRELOAD") 在任何 exec 调用之前执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM