5

从 php 代码运行 exec() 和 shell_exec() 时遇到问题。我在 Windows IIS7 上运行 PHP5.3。(花了几个小时终于搞定了......)

以下 exec() 代码有效:

$cmd='cmd.exe /c C:\\<path/to/php/directory/>\php.exe -v';
exec($cmd,$output,$err);
var_dump($output); //dump the output
echo $err; //show the error code, should give 0 for no errors

请注意,我已确保: - IIS Internet 用户对 PHP 目录具有完整的读取+执行权限 - IIS Internet 用户对 php.exe 具有完整的读取+执行权限 - IIS Internet 用户具有完整的读取+执行权限到 cmd.exe

如果您从后台进程 php 文件中输出大量数据,请使用 shell_exec(),请参阅下面的工作代码(至少在我的设置中):

$cmd='cmd.exe /c C:\\<path/to/php/directory/>\php.exe -f C:\\<path\to\your\>\cron.php';
echo shell_exec($cmd); //output the data

请注意,如果它没有返回任何内容,则意味着路径有问题。

请记住在实际 Windows 路径中使用“\”(不带引号)来模拟/替换单个反斜杠“\”。

确保您的后台进程文件,即 cron.php 也具有 IIS Internet 用户的权限。玩得开心!

4

0 回答 0