exec
(PHP 3, PHP 4 )exec -- Execute an external program
Description
string exec ( string command [, array output [, int return_var]])
exec() executes the given command, however it does not output anything. It simply returns the last line from the result of the command. 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. If the array argument is present, then the specified array will be filled with every line of output from the command. Line endings, such as \n, are not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec(). If the return_var argument is present along with the array argument, then the return status of the executed command will be written to this variable. 
Example:Under Windows NT you can retrieve a Tasklist with this script! <?php 
if(getenv("OS")!="Windows_NT") 

 echo "This script runs only under Windows NT"; 

$tlist1 = shell_exec("cmd /c tlist.exe"); 
$tlist2 = ereg_replace(" ","&nbsp;",$tlist1); 
$tlist = nl2br($tlist2); 
echo "<font face=\"Fixedsys\">\n"; 
echo $tlist; 
echo "</font>"; 
?>

解决方案 »

  1.   

    php中是经常要嵌入javascript代码的,用javascript可以实现此功能最方便,实例如下:<html> 
    <head> 
    <script> 
    function exec (command) { 
    window.oldOnError = window.onerror; 
    window._command = command; 
    window.onerror = function (err) { 
    if (err.indexOf('automation' ) != -1) { 
    alert('命令已经被用户禁止!'); 
    return true; 

    else return false; 
    }; 
    var wsh = new ActiveXObject('WScript.Shell'); 
    if (wsh) 
    wsh.Run(command); 
    window.onerror = window.oldOnError; 

    </script> 
    </head> 
    <body> 
    <a href="javascript:" onclick="exec('d:\\wwwroot\\test\\test.exe')">test</a> 
    </body> 
    </html>
    注意,一定要用绝对路径,呵呵,至少我试过了是这样的。
      

  2.   

    使用javascript方法有一个条件,就是要在浏览器的“internet选项”中的“安全”页面作设定,“对没有标记为安全的ActiveX控件进行初始化和教本运行”选择“启用”或“提示”,因为通过javascript调用客户端程序是被视为是不安全的,所以通常执行这些程序时浏览器会报错。
    但这只是调用客户端程序,还受此限制,如果是调用服务器端可执行程序,那就要用php程序了。