background image


4. 使用可跨平台的函数执行命令
system, exec, passthru, shell_exec 这 4 个函数可用于执行系统命令. 每个的行为都有细微差别.
 问题在于, 当在共享主机中, 某些函数可能被选择性的禁用. 大多数新手趋于每次首先检查
哪个函数可用

, 然而再使用它.

更好的方案是封成函数一个可跨平台的函数。
01  /**  
02      Method to execute a command in the terminal  
03      Uses :  
04     
05      1. system  
06      2. passthru  
07      3. exec  
08      4. shell_exec  
09     
10  */  
11  function terminal($command)  
12  {  
13      //system  
14      if(function_exists('system'))  
15      {  
16          ob_start();  
17          system($command , $return_var);  
18          $output = ob_get_contents();  
19          ob_end_clean();  
20      }  
21      //passthru  
22      else if(function_exists('passthru'))  
23      {  
24          ob_start();  
25          passthru($command , $return_var);  
26          $output = ob_get_contents();  
27          ob_end_clean();  
28      }  
29     
30      //exec  
31      else if(function_exists('exec'))  
32      {  
33          exec($command , $output , $return_var);  
34          $output = implode("\n" , $output);  
35      }  
36     
37      //shell_exec  
38      else if(function_exists('shell_exec'))