dongyi0114
2019-04-14 11:42
浏览 91
已采纳

如何为apache提供cmd权限?

I'm working on a php application that pulls github repositories to the server. Github webhooks call the php files.

I want to execute a cmd command using php. I assume I need apache permissions but I don't know how to give them.

The php code below creates an mkdir.bat and a gitclone.bat and runs them. The mkdir runs successfully, it creates an empty folder, but the gitclone doesn't create any folders or files. When I run the gitclone manually it does create folders and files.

file_put_contents("mkdir.bat", "mkdir test");
exec("mkdir.bat");

file_put_contents("gitclone.bat", "git clone https://github.com/gutyina700/WPTG.git");
exec("gitclone.bat");

图片转代码服务由CSDN问答提供 功能建议

我正在开发一个将github存储库拉到服务器的php应用程序。 Github webhooks调用php文件。

我想用php执行cmd命令。 我假设我需要apache权限,但我不知道如何给它们。

下面的php代码创建了一个 mkdir.bat 和一个 gitclone .bat 并运行它们。 mkdir 成功运行,它会创建一个空文件夹,但 gitclone 不会创建任何文件夹或文件。 当我手动运行 gitclone 时,它会创建文件夹和文件。

  file_put_contents(“mkdir.bat”,“mkdir test”); 
exec  (“mkdir.bat”); 
 
file_put_contents(“gitclone.bat”,“git clone https://github.com/gutyina700/WPTG.git");
exec("gitclone.bat");
    
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqotv26286 2019-04-14 12:58
    已采纳

    I executed your code in my computer

    file_put_contents("gitclone.bat", "git clone https://github.com/gutyina700/WPTG.git");
    exec("gitclone.bat 2>&1",$o);
    print_r($o);
    

    the extra code is to check the output from cmd and it returned "git command not exsists" so you only have to add a line as

    <?php
    putenv("PATH=C:\Program Files\Git\cmd");
    file_put_contents("gitclone.bat", "git clone https://github.com/gutyina700/WPTG.git");
    exec("gitclone.bat 2>&1",$o);
    print_r($o);
    ?>
    

    This was working properly.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题