Search code examples
phpwindowsgitbatch-filegit-push

How to push git from php using exec()


Here is my push.bat file

echo "Hello world!"
cd abhishek3/
call git add .
call git commit -m "sadf"
call heroku accounts:set abhishek84 
call git push heroku master

I am able to push to my repository by running push.bat file in cmd

I want to push to my repo from php, tried following snippets but none of them worked for me.

exec("psexec -d push.bat");

Result: PHP page loads indefinitely

$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("cmd /C abhishek3.bat, 0, false);

Result: Page stops loading after a while and nothing works

exec("cmd.exe /c abhishek3.bat")

Result: Page stops loading after a while and nothing works

EDIT: Tried the following snippet but nothing worked.

exec("hstart.exe /NOCONSOLE \"cmd.exe /c \"abhishek3.bat\"\"");

The above snippet runs successfully when run from cmd but when run from php through exec() didn't worked. Download hstart (Hidden Start Binary)


Solution

    1. check the permissions for the web users are correct (It can access the files and execute git)
    2. check the path for git and any environment variables it may need. You may have to set those environment variables in your script and use absolute paths)
    3. check IIS or apache (whichever you are using) is allowed to execute programs in general and bat files (for example if it is apache, you may have to configure httpd.conf or htaccess depending on your configuration)