views:

366

answers:

2

Hi there,

I'm trying to use the PHP exec() or system() (or any other similar function) to run a batch file, but I can't seem to get these to return anything.

The simplest example I've seen is this, which outputs nothing:

<?php
    echo system('dir');
?>

The script is running on a windows XP machine on IIS with PHP installed and I've also tried it on my shared hosting account running windows 2003 server/IIS.

Can anyone suggest what I need to do to get this working, or provide any commands I can use for troubleshooting?

Cheers,

Tom

Edit: second example

Based on pavun_cool's answer I tried the following:

<?php
    $last_line = system('dir', $retval);
    echo 'last_line '.$last_line.'<br/> retval '.$retval;
?>

The output is:

last_line
retval -1

Edit: third example

Based on Manos Dilaverakis I tried the following code

<?php
exec('dir', $response);
foreach($response as $line) {
    echo $line . "<br>";
}
?>

The output is:

<br>

I.e. a blank line when displayed in a browser.

Also looking in php.ini, the following line (which presumably could disable these functions) is empty:

disable_functions =

Does anyone have any further suggestions or anything else I can try?

+1  A: 

For getting return values , you need to pass the second argument for system function .

$last_line = system('ls', $retval);

Here $retval will have the return value of the ls execution.

pavun_cool
I tried this and added it to my question above - $retval is -1 rather than the output I would expect.
Loftx
A: 

Here, if this doesn't work, then exec's probably disabled in php.ini, which means you'll have to change your PHP configuration

<?php
exec('dir', $response);
foreach($response as $line) {
    echo $line . "<br>";
}
?>
Manos Dilaverakis
I've just tried this, and the output is simply <br> also there don't appear to be any functions disabled in php.ini.
Loftx