Open batch file php

Before everyone starts butting in with "security risks" "cant be done" stop there and read the ENTIRE post

I have a web server set up from a home laptop which is serving as a games web server im trying to create a GUI so its easier for us to maintain the server and im trying to use batch files to do the actions on the computer

So to put this into perspective I have my index file 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 running the server and will do all the actions nesscary to start our game server so there is another directory "Instance" containing an excutable "Server.exe" which the batch file will run

The issue im having is running the web server and testing this it doesnt work if I open the batch file directly it works but it seems the php code doesnt work

For clarification I am using apache and my browser is chrome

And just a quick question for anyone willing to answer the route im going is correct right? Using php would allow everything to run on the machine hosting the server so the end user will only see the GUI and the server would run the batch files and everything on the web server and not the local machine if that makes sense?

EDIT: To be more clear about what's going on the function exec runs but it just hangs like the application is loading I need a solution that will actually open the application are my host computer for example if I wanted to open up notepad I press a button on the Web server and notepad will open on the computer

EDIT 2: I would like to note that I dont exactly 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 on the sorts echo exec('start text.bat'); I will get a This is a test to show your batch is working and simply just have echo ..... in the batch file the main issue I am having is the server is not physically showing the opened file like displaying the GUI lets just take notepad for example

I can open notepad and get some return value as long as my batch file closes notepad once its finished running however the GUI for notepad is never displayed and thats very important

I read in a few articles about using apache as a service which im pretty sure I am but I do know that xaammp has suffiecient priveleges and I have checked the box that says "Allow apache to interact with desktop" however no GUI is popping up thats the main point I guess im trying to get across is I need to display the GUI not just open the file as a background service.

If it makes answering easier I am open to switching programming languages if theres one that can do what I want easier


In php manual about exec function, there is a note :

Note: If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

I think "hangs like the application is loading" is your application waiting for the bat file terminated / closed to get the output result

Let's try another approach, i found it here, the concept is to create a scheduler that execute the program you want and call it using 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');

Your theory is correct, it will run on the server however you may have issues running applications directly from php (with this method afaik it does not detach from the PHP, and the webapp "hangs" while the application is running).

Make sure: return values are printed / logged. Just an

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

Could point you to the right direction. The exec function may have been disabled in your distribution.

Using

<?php

instead of

<? 

is highly advised, by default short_tags are not enabled 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 any response, try a simple batch file with a "hello world" to test if it works.

Be aware, the rights and limitations are comes from the php environment, the batch file inherits the same rights running the PHP code / Apache (in case of mod_php)


From here:

How do you run a .bat file from PHP?

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

链接地址: http://www.djcxy.com/p/96644.html

上一篇: 以编程方式在Google Spreadsheet Chart中设置最小值

下一篇: 打开批处理文件php