PHP shell_exec only works for certain commands

2020-07-22 09:46发布

问题:

I am a complete PHP (LAMP/WAMP/MAMP) noob and I'm running into what seems to be a common issue among noobs trying to run shell commands from a web server. I have a MAMP setup on a Mac mini and I want to collect the output from an Android command,

adb devices

so I can display a list of devices attached to my mac mini. whenever I pass the command to shell_exec in my PHP script I get an empty string returned. I cannot figure out how to resolve the issue after several web searches. I tried running

shell_exec("who am i")

which returns "Clifton", the name of the user account that launched MAMP. I verified that this same user account can execute the "adb devices" command. The Android tools are installed in that user account's home folder. I've also tried passing the absolute path to the command. At this point, I am certain that is a configuration issue but I don't know what to configure or where.

回答1:

Try this:

shell_exec("adb devices  2>&1")

Source: http://www.php.net/manual/en/function.shell-exec.php#106250



回答2:

You should try to open the file /Applications/MAMP/Library/bin/envvars and then comment the four lines of if and else. This is looks like this:

#if test "x$DYLD_LIBRARY_PATH" != "x" ; then
#  DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
#else
#  DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib"

It works fine for me! I hope that you can enjoy it. Good Lucky!



标签: php mamp