0

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.

jww
  • 97,681
  • 90
  • 411
  • 885
kisna
  • 55
  • 3
  • 11
  • The use of `$_POST` suggests HTTP. There's no practical way to call an interactive program using server-side PHP. Even if you manage to launch a `cmd.exe` window (something that I'm not fully sure is possible) the window would popup on the server, so you'd need to log-in with the user the web server runs as. – Álvaro González Aug 06 '13 at 11:00
  • 1
    What makes you think a cmd window should appear? the command probably runs (once you've sorted out the path and all that), but that doesn't mean you'll _see_ a window appear. Your bat file also suggest some form of interaction is required, which can't be done using `exec`, you'll have to use `proc_open` or something, to have input and output pipes – Elias Van Ootegem Aug 06 '13 at 11:05
  • i using wamp server for this – kisna Aug 06 '13 at 11:05
  • @ÁlvaroG.Vicario PHP offers great interoperbility with other shells through `shell_exec`, `system`, etc. and even backticks. Even it is more common on linux server, it works well on windows too. You don't need the Shell window to appear, you just need to capture its STDOUT/STDERR – Daniel W. Aug 06 '13 at 11:06
  • @EliasVanOotegem:i dont want to pop up cmd window..what i want is the actions which are written in .bat file has to be done – kisna Aug 06 '13 at 11:06
  • 1
    @krishnabhargavi: Then you'll have to use `proc_open` and use pipes, that reference the STDIN and STDOUT streams, so you can actually respond to the bat script – Elias Van Ootegem Aug 06 '13 at 11:12
  • @EliasVanOotegem - What made me think a window should appear? The "has to open the command prompt" part of the question, together with the interactive batch file that even ends with `pause`. My assumption was wrong but the question doesn't make it easy... – Álvaro González Aug 08 '13 at 07:32
  • @ÁlvaroG.Vicario: I wasn't asking what made _you_ think the prompt should open, but the OP seemed to think that, since he doesn't see a prompt pop up, the `exec` call isn't working... or at least, that's how I understood the question. – Elias Van Ootegem Aug 08 '13 at 07:36
  • Possible duplicate of [How do you run a .bat file from PHP?](http://stackoverflow.com/questions/835941/how-do-you-run-a-bat-file-from-php) – jww Nov 15 '16 at 03:54

4 Answers4

2
$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.

Daniel W.
  • 31,164
  • 13
  • 93
  • 151
1

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.

g4vroche
  • 527
  • 3
  • 11
0

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.

Huck c
  • 102
  • 4
  • You can see in my main.bat file when the user clicks choice 1 it goes to append.bat..in append.bat i have log.txt file.... – kisna Aug 06 '13 at 11:26
0

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);
}
Álvaro González
  • 142,137
  • 41
  • 261
  • 360