PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, March 10, 2022

[FIXED] Run shell command in PHP from MAMP

 March 10, 2022     bash, macos, mamp, php, shell     No comments   

Issue

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

Solution

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


Answered By - Vineet1982
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing