0

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
2
  • What do you expect your command to return? shell_exec() returns null on both error or no output. Commented Nov 15, 2013 at 12:10
  • I put output to test the outcome, I only want to execute the command Commented Nov 15, 2013 at 12:39

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

5 Comments

So what is a safe practice in running shell command in mac? Its a necessary part of my web page to use a shell command.
I can't find shell_exec in php.ini, also I have my disabled functions with no parameters in php.ini
@fareed answered your question also
@fareed did you find any solution for execution shell_exec ?
@Akhtar yes! I have. Alter your php.ini to provide access to shell

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.