问题
I am trying to execute a batch file using PHP on Windows Server 2008.
The file is called NEWDNS.bat
and its purpose is to write a new entry into a DNS zone file stored in c:\windows\system32\dns
. The zone file is named motlocal.co.uk
.
Here are the contents of newdns.bat
@echo off
cd windows\system32\dns\
dnscmd dsvr012345 /recordadd motlocal.co.uk mynewsubdomain A 88.208.200.221
dnscmd dsvr012345 /ZoneReload motlocal.co.uk
newdns.bat is stored in the same directory on the server as the PHP page that calls it:-
Here's the contents of the page that calls newdns.bat (the php page is called dnscreator.php)
<?php
if(isset($_POST['submit']))
{
echo exec("cmd.exe /c newdns.bat");
echo "Done!";
} else {
?>
<form action="" method="post">
<input type="submit" name="submit" value="DO IT!">
</form>
<?php
}
?>
When I double click on the batch file from a windows folder - it runs and it executes its commands perfectly and creates the new subdomain in the dns zone file.
However - no matter how I try (and I have been on this for 2 days now) it it will NOT execut when I run dnscreator.php.
The form executes and displays the message "Done!" but the commands in the newdns.bat have not been carried out. I know the batch file works because as I said - I can run it from a windows folder (Double Click) and it does what its supposed to do.
PHP Safe mode is OFF, just in case you were wondering and I have tried moving the batch file into c:\windows\system32 and this didn't work either.
Any help / advice / pointers you can give is greatly appreciated - I am at my wits end! Many Thanks.
回答1:
If your PHP Page is located in the same directory as the batch file, then the following wil do it:
exec("batchname.bat");
Reference;
Personal experience
回答2:
1 - cd windows\system32\dns\
CD from where? it is a relative path from the current directory of the process calling the batch file
2 - You can run the batch file, but, can the account (under which the web server is running) do it?
3 - How do you know the batch file is running or not? Try to leave a log from inside the batch file ( echo inside batch > "%temp%\batchphp.log"
)
来源:https://stackoverflow.com/questions/20401352/php-not-executing-bat-file-in-windows