1

I am trying to do the following in my php file:

exec("java -jar /Applications/XAMPP/htdocs/Web/SimpleEncryption.jar hola", $output);

I get array(0) { }

If I execute php file.php in the terminal it works.

If I run

exec("whoami", $result);

I get Array ( [0] => daemon ) in the browser and Array ( [0] => dao ) in the terminal. dao=my user.

I don't know what permissions I have to give and how to give them.

Thanks

  • 1
    The webserver user (for example www-data for apache) does not have the permissions to execute shell commands. – Tyr Dec 27 '14 at 15:50
  • Make sure the user `daemon` knows where to find `java`. – axiac Dec 27 '14 at 15:50
  • `java` probably isn't in the webserver's `$PATH`. Use the full path to the command. – Barmar Dec 27 '14 at 15:51
  • Thanks, I added the Path to java... Now I get the following error: Error occurred during initialization of VM" [1]=> string(45) "Unable to load native library: libjava.jnilib Someone said in another post to put this: exec('export DYLD_LIBRARY_PATH=""; /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java -version'); but it didn't work. Do you now how to solve it? I am using MAC – user3171362 Dec 28 '14 at 00:24

1 Answers1

1

It is probably that the username of the php server process is not your username. When you run it from the terminal you are logged in as dao, but the php process served up by your webserver is under a different username. If you are using php-fpm the username is specified in the conf file.

bspates
  • 382
  • 1
  • 10
  • I'm using XAMPP, where is the config file? – user3171362 Dec 27 '14 at 16:13
  • Now I have the same answer of whoami in the browser and in the terminal... but still doesn't work. – user3171362 Dec 27 '14 at 16:22
  • Depending on the version of php your using (< 5.3 I think ) you may have to disable safe mode. http://stackoverflow.com/questions/24999673/how-to-enable-shell-exec-and-exec-on-php. Or you need to remove exec from the list of removed functions http://www.cyberciti.biz/faq/linux-unix-apache-lighttpd-phpini-disable-functions/ – bspates Dec 27 '14 at 16:32
  • I have done both safe_mode=Off disable_functions="" in php.ini – user3171362 Dec 27 '14 at 17:17
  • What is the output of `echo shell_exec("java -version");` in the browser? – bspates Dec 27 '14 at 19:12