简体   繁体   中英

Run shell command in PHP from MAMP

I have installed MAMP on my OSX and tried to run a shell command from my php file but it seems I can't access the terminal or the command is not being sent to terminal.

$cmd = "some shell command"
$output = shell_exec($cmd);
if($output == null)
    echo "returned null";
else echo $output;

I'm always getting "returned null" and there shell command is not being executed. The command is correct, if I copied "some shell command" and paste it in terminal, the command gets executed. I have tested the same file in Windows and the command line is executed from php, this problem only appears in mac. So how can I access terminal in mac?

PS:

  • OSX 10.6.8
  • MAMP 2.0.5
  • PHP 5.3.6

There is lot of difference between Windows/Mac/Linux OS working. In Windows shell_exe command has provided the access. Even if the user has limited resources in Windows the shell_exe command can be execute.

In case of Mac/Linux OS there are lot of restrictions on running shell_exe commands. It is also highly recommended not to shell_exe commands from PHP. The host machine will become target easily. You have to edit the PHP.INI for providing shell_exe access to the user.

If you are not admin of host machine of Mac/Linux then ask the Machine admin to do so. If, Admin Provides access then ok less you have to try another way around and that depends on your project requirements.

Editing PHP.INI

Which is the correct PHP.INI file for editing depends on the OS used by Machine. The location generally changed according to the required things.

If you are using WHM CPanel then it becomes little easy. The general location of php.ini file

 /usr/local/lib

and if you are using any other 3rd Party Program the location would generally changed to

/usr/local/cpanel/3rdparty/etc/php.ini

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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