5

I tried to execute the batch file using exec command in PHP. I just used it like:

$filename = 'test.bat';
exec($filename);

But didn't get any output. I tried this function with another command, it works fine. Your suggestions would be highly appreciated. Thanks

1
  • 1
    you should probably set the path on the $filename. maybe it didn't find the path of the file. Commented Sep 30, 2013 at 7:12

6 Answers 6

7

The main issue was of path and permission. I have gotten my batch file to execute.

Here is my solution:

  1. I run my batch file from the same folder the php file is in.

    exec("mybatch.bat");

  2. I make sure that Apache Service has enough permission to run the batch file. Just to test i used an administrator account for Apache to log on with.

Sign up to request clarification or add additional context in comments.

1 Comment

Correct solutions, Both Batch file and php files are in same folder
2

On Windows server mind the quotes. This is what works for me:

system('cmd.exe /c C:\myfolder\_batches\run_this_batch.bat');

Comments

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

As "RichieHindle" said in a similar topic.

or try

exec("cmd.exe /c test.bat") ?

Comments

1

If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.

https://www.php.net/manual/en/function.passthru.php

Comments

1

What I did was the following:

  1. created a PHP file that contained :

    $gotIt = array();
    $file = "getMyIP.bat";
    exec( $file, $gotIt );
    echo implode("<br>",$gotIt);
    
  2. Created a batchfile in the same folder

    @ECHO off
    ipconfig
    
  3. Ran it and waited for the firewall to jump all over the action.

I then got an output like :

Windows IP Configuration


PPP adapter 3 USB Modem:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : ***.***.202.81
Subnet Mask . . . . . . . . . . . : 255.255.255.255
Default Gateway . . . . . . . . . : ***.***.202.81

only theres numbers where the ***'s are

Comments

0

As it is explained in the exec doc:

echo exec($filename);

or

exec($filename, $output);
echo $output;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.