Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

illegal command error code 127 in php exec function

Tags:

php

exec

I am using this php code:

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?

like image 620
Intellex Avatar asked Jan 13 '09 10:01

Intellex


5 Answers

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

like image 153
AAA Avatar answered Nov 17 '22 01:11

AAA


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

; Safe Mode
; http://www.php.net/manual/en/ini.sect.safe-mode.php#ini.safe-mode
safe_mode = Off
like image 32
Brandon Avatar answered Nov 17 '22 01:11

Brandon


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.

like image 23
Rogelio Triviño Avatar answered Nov 17 '22 01:11

Rogelio Triviño


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

like image 2
Intellex Avatar answered Nov 17 '22 02:11

Intellex


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

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.

like image 1
ChrisH Avatar answered Nov 17 '22 01:11

ChrisH