1

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?

2
  • 1
    ...PHP's manual pages on exec and system answer this. You don't even need to scroll down Commented Apr 2, 2010 at 11:38
  • 1
    @Manos Dilaverakis - could you be a bit more specific? The text from the exec function states "Return Values: The last line from the result of the command." - so shouldn't I see the last line of output from whatever I run run? Commented Apr 2, 2010 at 11:58

3 Answers 3

1

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.

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

1 Comment

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

When working with windows and PHP, several commands (including dir, and other windows commands such as ping) do not output their return values properly to the php.exe binary. For example, trying to get dir to produce output with a PHP shell_exec() function will always result in "false". If you attempt to give it a value like dir c:\ it will result in: NULL which is similar to FALSE in that it provides nothing useful. The ONLY workaround is to use php's built in scandir() and make your own version of dir manually with PHP.

Comments

0

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>";
}
?>

1 Comment

I've just tried this, and the output is simply <br> also there don't appear to be any functions disabled in php.ini.

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.