简体   繁体   English

从MAMP在PHP中运行shell命令

[英]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. 我已经在我的OSX上安装了MAMP并尝试从我的php文件运行shell命令,但似乎我无法访问终端或命令没有被发送到终端。

$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. 我总是得到“返回null”并且没有执行shell命令。 The command is correct, if I copied "some shell command" and paste it in terminal, the command gets executed. 命令是正确的,如果我复制了“some shell command”并将其粘贴到终端,则执行该命令。 I have tested the same file in Windows and the command line is executed from php, this problem only appears in mac. 我在Windows中测试了相同的文件,命令行是从php执行的,这个问题只出现在mac中。 So how can I access terminal in mac? 那么如何在mac中访问终端?

PS: PS:

  • OSX 10.6.8 OSX 10.6.8
  • MAMP 2.0.5 MAMP 2.0.5
  • PHP 5.3.6 PHP 5.3.6

There is lot of difference between Windows/Mac/Linux OS working. Windows / Mac / Linux OS工作之间有很多区别。 In Windows shell_exe command has provided the access. 在Windows中,shell_exe命令提供了访问权限。 Even if the user has limited resources in Windows the shell_exe command can be execute. 即使用户在Windows中具有有限的资源,也可以执行shell_exe命令。

In case of Mac/Linux OS there are lot of restrictions on running shell_exe commands. 在Mac / Linux OS的情况下,运行shell_exe命令有很多限制。 It is also highly recommended not to shell_exe commands from PHP. 强烈建议不要使用PHP的shell_exe命令。 The host machine will become target easily. 主机将很容易成为目标。 You have to edit the PHP.INI for providing shell_exe access to the user. 您必须编辑PHP.INI以提供对用户的shell_exe访问。

If you are not admin of host machine of Mac/Linux then ask the Machine admin to do so. 如果您不是Mac / Linux主机的管理员,请让机器管理员这样做。 If, Admin Provides access then ok less you have to try another way around and that depends on your project requirements. 如果,管理员提供访问权限,那么你必须尝试另一种方式,这取决于你的项目要求。

Editing PHP.INI 编辑PHP.INI

Which is the correct PHP.INI file for editing depends on the OS used by Machine. 哪个用于编辑的正确PHP.INI文件取决于Machine使用的操作系统。 The location generally changed according to the required things. 位置通常根据所需要的东西而改变。

If you are using WHM CPanel then it becomes little easy. 如果您正在使用WHM CPanel,那么它就变得不那么容易了。 The general location of php.ini file php.ini文件的一般位置

 /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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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