Reputation: 55
I need to run a .bat
file in a command prompt whenever I click a button or hyperlink. The code I've written is:
<?php
if(isset($_POST['submit']))
{
$param_val = 1;
$test='main.bat $par';
// exec('c:\WINDOWS\system32\cmd.exe /c START C:/wamp/www/demo/m.bat');
// exec('cmd /c C:/wamp/www/demo/m.bat');
// exec('C:/WINDOWS/system32/cmd.exe');
// exec('cmd.exe /c C:/wamp/www/demo/main.bat');
exec('$test');
}
else
{
?>
<form action="" method="post">
<input type="submit" name="submit" value="Run">
</form>
<?php
}
?>
my main.bat
is:
@echo off
cls
:start
echo.
echo 1.append date and time into log file
echo 2.just ping google.com
set/p choice="select your option?"
if '%choice%'=='1' goto :choice1
if '%choice%'=='2' goto :choice2
echo "%choice%" is not a valid option. Please try again.
echo.
goto start
:choice1
call append.bat
goto end
:choice2
call try.bat
goto end
:end
pause
When I click the run button it has to open the command prompt and run the main.bat
file, but whenever I click run it says nothing.
Upvotes: 0
Views: 6272
Reputation: 146460
Assuming that you just want to simulate an interactive session, you just need to use proc_open() and related functions:
<?php
$command = escapeshellcmd('main.bat');
$input = '1';
$descriptors = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
);
$ps = proc_open($command, $descriptors, $pipes);
if(is_resource($ps)){
fwrite($pipes[0], $input);
fclose($pipes[0]);
while(!feof($pipes[1])){
echo fread($pipes[1], 4096);
}
fclose($pipes[1]);
$output = proc_close($ps);
if($output!=0){
trigger_error("Command returned $output", E_USER_ERROR);
}
}else{
trigger_error('Could not execute command', E_USER_ERROR);
}
Upvotes: 0
Reputation: 102
I‘ve tried this exec on my pc . your bat would executed but you can't see the black interface. you could try the bat like @echo off Echo tmptile > tmp.txt like this ,it could create the a file named tmp.txt which tells you. the bat was executed.
Upvotes: 0
Reputation: 32300
$test='main.bat $par';
exec('$test');
... won't work.
PHP only takes $variables in double quotation marks.
This is bad practice also: $test = "main.bat $par";
.
Also windows takes backslashes instead of slashes which need to be escaped through another backslash in double quotes.
Use one of these:
$test = 'cmd /c C:\wamp\www\demo\main.bat ' . $par;
or
$test = "cmd /c C:\\wamp\\www\\demo\\main.bat {$par}";
run:
echo shell_exec($test);
Even more fails:
Remove the pause
from the end of your script. PHP does not get arround that automatically.
Looking more at the batch file, I bet you don't even need it. Everything inside the batch file can be put into a PHP file.
As Elias Van Ootegem already mentioned, you would need to pipe in STDIN to enter your option (1, 2) into the batch file.
Upvotes: 2
Reputation: 527
Since you run the PHP script through a browser, on a web server, the .bat file execution occurs on the web server not the client.
No matter if you run your server on the same computer, your bat may be executed but you can not interact with it.
The solution may be to make a bat that takes arguments instead of being interactive, and bring the interaction back to front the PHP script in order to call the bat execution with the correct args.
Upvotes: 1