PHP exec() and system() functions always return false in IIS
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 s开发者_运维技巧ystem('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?
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.
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.
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>";
}
?>
精彩评论