Open php batch file

Before everyone starts attacking with “security risks,” “impossible to do,” stop there and read the ENTIRE post

I have a web server configured from my home laptop, which serves as a web server for games trying to create a graphical interface, so it’s easier for us to support the server and try to use batch files to perform actions on the computer

So, to put this in perspective, I have an index index index.php

<form method="post"> <input type="submit" name="startServer" value="Start Server"> </form> <? if(isset($_POST['startServer'])){ exec('batch/startServer.bat'); } ?> 

And my startServer.bat will run on the laptop on which the server is running, and will perform all the steps necessary to start our game server, so there is another directory "Instance" containing excutable "Server.exe", which will run the command file

The problem is when starting the web server, and testing this does not work, if I open the batch file directly, it works, but it looks like the PHP code does not work

For clarification, I use apache and my browser is chrome

And just a quick question for those who want to answer the route that goes, right? Using php will allow you to run everything on the machine where the server is located, so the end user will see only the graphical interface, and the server will run batch files and everything on the web server, and not on the local machine, if that makes sense?

EDIT: To be more clear what happens with exec exec functions, but it just hangs as the application loads. I need a solution that will actually open the application, this is my main computer, for example, if I wanted to open notepad I press a button on the web server and notepad will open on the computer

EDIT 2: I would like to point out that I do not need the need to use the exec function, and I have tried all the answers to date. 7/19/2017: 3: 45pm none are working if I do something in sortings echo exec('start text.bat'); , I will get This is a test to show your batch is working and just just echo ..... in the batch file, the main problem I am facing is that it doesn’t physically show the open file, like GUI display just take a notepad, eg

I can open the notepad and get some return value while my batch file closes the notepad after it is completed, however the graphical interface for the notepad is never displayed, and this is very important

I read in several articles about using apache as a service, which I'm sure I have, but I know that xaammp has privilege privileges, and I checked the box that says "Allow apache to interact with the desktop," however The GUI is missing popping thats the main point, I assume that I am trying to understand, I need to display the GUI, and not just open the file as a background service.

If it makes the answer easier, I'm open to switching programming languages, if one that can do what I need is easier

+7
windows php batch-file
source share
8 answers

There is a note in the php manual about exec :

Note. If a program starts with this function so that it continues to run in the background, the program output must be redirected to a file or other output stream. Otherwise, make PHP hang until the program completes.

I think that “freezes as the application loads” is your application, expecting the bat file to disappear / close to get the output result

Try a different approach, I found here here , the concept is to create a scheduler that runs the program you need and invoke it with a command.

hope this help:

 shell_exec('SCHTASKS /F /Create /TN _notepad /TR "notepad.exe" /SC DAILY /RU INTERACTIVE'); shell_exec('SCHTASKS /RUN /TN "_notepad"'); shell_exec('SCHTASKS /DELETE /TN "_notepad" /F'); 
+1
source share

Your theory is correct, it will work on the server, however, you may have problems launching applications directly from php (using this afaik method, it does not disconnect from PHP, and webapp freezes while the application is running).

Make sure: the return values ​​are printed / logged. Just

 <?php if(isset($_POST['startServer'])){ echo exec('batch/startServer.bat'); } ?> 

May indicate the right direction. The exec function may be disabled in your distribution.

Using

 <?php 

instead

 <? 

highly recommended, by default short_tags are not included in most distributions (wamp, xamp, etc.).

Set debug mode and print everything to get information about the problem:

 <?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); if(isset($_POST['startServer'])){ echo exec('batch/startServer.bat'); } ?> 

If you don't have an answer, try a simple batch file with "hello world" to see if it works.

Know that the rights and restrictions come from the php environment, the batch file inherits the same rights as the PHP code / Apache (in the case of mod_php)

+3
source share

From here:

How to run a .bat file with PHP?

Have you tried: system("cmd /c C:[path to file]"); ?

0
source share

You may need to run it through cmd, for example:

 system("cmd /c C:[path to file]"); 

Or try the following options

one.

 <?php exec('c:\WINDOWS\system32\cmd.exe /c START C:\Program Files\VideoLAN\VLC\vlc.bat'); ?> 

2.

When you use the exec() function, it is like opening a CMD terminal and writing commands directly.

Use single quotes like this $str = exec('start /B Path\to\batch.bat'); /B means that the bit will be executed in the background, so the rest of php will continue after running this line, unlike $str = exec('start /B /C command', $result); where command is executed and then result saved for later use.

 <?php pclose(popen("start /B test.bat", "r")); die(); ?> 
0
source share

I think this is a containment problem.

if you run an application running php running iiswebuser when php shuts down, it will close all child processes generated by it in Windows. there is a very fast command to retrieve an application from the contents of a child process using the start command.

 if(isset($_POST['startServer'])){ exec('start batch/startServer.bat'); } 

Containment scheme, as I explained (simplified)

  • IIS (IIS works as IISUser)
    • php (application)
      • cmd.exe (package)

using start, bring it to the root of this tree

  • IIS (IIS works as IISUser)
    • php (application)
  • cmd.exe (package)
0
source share

Baim Wrong was correct in the first part of the answer: you need to redirect the output of the script or your PHP code will hang. In addition, you need to move the process in the background.

This is easy to do on * nix:

 system("/usr/local/bin/shell.sh >> /tmp/log.log 2>&1 &"); 

I know that you can redirect output to Windows, but don’t know how to move the process in the background. You should check the DOS manual or try using the power shell.

0
source share

you can use either system or exec php function

 $path = __DIR__ . '/batch/startServer.bat'; exec('cmd /c start ' . $path); 

or

 $path = __DIR__ . '/batch/startServer.bat'; $lastLine = system('cmd /c start ' . $path); 
0
source share

You have a problem launching the application directly from exec. I had the same problem running a file using exec. It was solved by passing another parameter 2> & 1.

 exec('some_command 2>&1', $output); print_r($output); // to see the response to your command 

Check the values ​​printed on the output see the exec function

 <?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); $output = array(); if(isset($_POST['startServer'])){ exec('batch/startServer.bat 2>&1', $output); print_r($output); } else { echo "Not posted"; } ?> 
0
source share

All Articles