php exec() 命令权限被拒绝

2022-01-04 00:00:00 permissions exec linux php

我有一个 C++ 可执行文件skypeforwarder".如果我在 Mac 的终端中使用命令行,skypeforwarder 可以工作:henry$/Users/henry/Desktop/skypeForwarder/skypekit-sdk_sdk-4.1.2.20_793394/examples/cpp/tutorial/step3/skypeForwarder

I have a C++ executable file 'skypeforwarder'. skypeforwarder works if I use command line in Terminal in Mac: henry$ /Users/henry/Desktop/skypeForwarder/skypekit-sdk_sdk-4.1.2.20_793394/examples/cpp/tutorial/step3/skypeForwarder

sh: /Users/henry/Desktop/skypeForwarder/skypekit-sdk_sdk-4.1.2.20_793394/examples/cpp/tutorial/step3/skypeForwarder: Permission denied

但是如果在 php exec() 中调用它,它总是发出权限被拒绝";

But it always issued 'permission denied' if it is called in php exec();

<?php 
echo exec('whoami');

$output = null;

$execBuild = '/Users/henry/Desktop/skypeForwarder/skypekit-sdk_sdk-4.1.2.20_793394/examples/cpp/tutorial/step3/';
$execBuild .= 'skypeForwarder';

$n  = exec($execBuild, $output); 

我搜索了很多.问题应该是web服务器的php/browser权限问题.我还尝试从以下位置更改文件的所有者:

I searched a lot. The problem should be the problem of the php/browser permission in web server. I also tried to change the owner of the file from:

-rwxr-xr-x  1 henry  staff  1212716 19 Apr 11:23 skypeForwarder

-rwxr-xr-x  1 _www  staff  1212716 19 Apr 11:23 skypeForwarder

还是不行.

我根据 http://foundationphp.com/tutorials/php_leopard.php 在我的 mac 中设置了 apache

推荐答案

虽然文件本身可以被 web 服务器读取,但 Desktop 文件夹很可能不是,因此 web 服务器无法遍历进入它以定位可执行文件.您应该将 skypeforwarder 二进制文件移动到 Web 服务器可读的位置,例如与您尝试提供此 PHP 脚本的位置平行的目录.但是,该目录不应可通过网络访问.用 .htaccess 保护它或将它放在 Web DocumentRoot 之上,但它必须是 Web 服务器可读的.

Although the file itself is readable by the web server, the Desktop folder most likely is not, and the web server therefore cannot traverse into it to locate the executable file. You should move the skypeforwarder binary file into a location readable by the web server, such as a directory parallel to where you are attempting to serve this PHP script. That directory should not, however, be web-accessible. Protect it with .htaccess or place it above the web DocumentRoot, but it must be readable by the web server.

默认情况下,OSX 上的 Desktop-rwxr------ 并且不建议更改该目录的权限.

By default, Desktop on OSX is -rwxr------ and it is not advisable to change permissions on that directory.

此外,非常不建议将文件更改为由 _www 网络服务器用户拥有和写入.相反,它应该是 Web 服务器可读和可执行的,但不可写.

Moreover, it is very much not advisable to change the file to be owned and writable by _www the web server user. Instead, it should be readable and executable by the web server, but not writable.

chown henry skypeforwarder
chmod 755 skypeforwarder

标准免责声明:与往常一样,从可在网络上访问的 PHP 脚本执行系统调用时要格外小心.

Standard disclaimer: As always, be extremely cautious when executing system calls from PHP scripts accessible on the web.

相关文章