-1

SOLVED

First of all, I tried this all on a Windows 10 machine, and I expect a blank page if I don't use echo. But the code doesn't work.

<?php
exec ('cmd.exe server.bat 2>&1', $output);
print_r($output);
?>

output:

Array ( [0] => Microsoft Windows [Version 10.0.17134.228] [1] => (c) 2018 Microsoft Corporation. All rights reserved. [2] => [3] => C:\wamp64\www\server> )

<?php
exec ("cmd.exe server.bat 2>&1", $output);
print_r($output);
?>

A subdirectory or file .exe already exists. Error occurred while processing: .exe. A subdirectory or file server.bat already exists. Error occurred while processing: server.bat.

This bat file is in the same directory as the PHP script.

@echo on
cd /
cd wamp64
cd www
cd minecraft-server-stuff
start java -Xms1024M -Xmx16G -jar server.jar

PHP itself is enabled in httpd-conf. I tried Apache as admin ( my Microsoft account name), this does change the executer to me. Safe_mode is not available as an option so I can't disable it( I assume that not available means not active), maybe because it was removed in PHP 5.4. Currently using PHP 7.something

I am using wampserver, which is using the .ini of php5.3.44 I tried setting the user in the httpd-conf as me, I have little coding experience so I don't know how to verify if that works. I tried setting Apache as my account, this appears to set Apache as my account.

I currently don't have access to the logs( not that it logs anything but actual code things, like syntax error and such) I also don't have access to any other code, due to the fact that I am on work.

how i got it to work. the bat file reads the contents of a txt file if the content matches the set string the code executes.

contents of the .bat file

@echo off
:b
:: delims is not import as token, a is input 
set a=test
for /f "delims=" %%a in (your-txt-file-here.txt) do (
type nul >"your-txt-file-here.txt"
start java -Xms1024M -Xmx1G -jar forge-1.7.10-10.13.4.1614-1.7.10-universal.jar
)
::sleep voor 5 sec
PING -n 5 127.0.0.1>nul
goto b

contents of the .php file

<?php
$filename = 'your-txt-file-here.txt';
$somecontent = "test";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($somecontent) to file ($filename)";

    fclose($handle);

} else {
    echo "The file $filename is not writable";
}
?>
Naatilus
  • 1
  • 1
  • 1
    `But the code doesn't work.`, what doesn't work? How doesn't it work? Can you share more code please? – Alexandre Elshobokshy Aug 28 '18 at 07:15
  • The bat file doesn't get executed. If I call with $output in the command I get string in array warning so I'm not sure how it doesn't work. The contents of the bat file is this : cd / cd wamp64 cd www cd server start java -xms1024 -xmx16g -jar server.jar – Naatilus Aug 28 '18 at 07:44
  • 1
    Possible duplicate of [How do you run a .bat file from PHP?](https://stackoverflow.com/questions/835941/how-do-you-run-a-bat-file-from-php) – Alexandre Elshobokshy Aug 28 '18 at 07:50
  • Maybe, it's a bit different though. – Naatilus Aug 28 '18 at 07:56
  • 1
    It does not seem to be different :) I think @IslamElshobokshy is right. – Gabriel Aug 28 '18 at 08:01
  • I guess so. The reason why I didn't agree at first was the lack of information on the other post. – Naatilus Aug 28 '18 at 08:25
  • @Naatilus you might not find the same exact answer you're looking for on the web, but you'll need to adapt the answers you find to your own problem, you can't expect to find everything you're looking for ^^ – Alexandre Elshobokshy Aug 28 '18 at 08:38
  • @Naatilus please don't update the question with the answer. Post the answer apart from the question and mark it solved your problem. – Mike Doe Aug 29 '19 at 15:09

1 Answers1

0

A .bat file is not an executable in itself. I suppose that the exec instruction is not able to run this as a system program. You probably need to invoke the actual command interpreter (cmd.exe) to which you pass, as an argument, the name of your .bat file.

Gabriel
  • 1,401
  • 13
  • 21