php 执行命令函数

/**
    
Method to execute a command in the terminal
    
Uses :
 
    
1. system
    
2. passthru
    
3. exec
    
4. shell_exec
 
*/
function terminal($command)
{
    
//system
    if(function_exists('system'))
    {
        ob_start();
        system($command , $return_var);
        $output = ob_get_contents();
        ob_end_clean();
    }
    
//passthru
    else if(function_exists('passthru'))
    {
        ob_start();
        passthru($command , $return_var);
        $output = ob_get_contents();
        ob_end_clean();
    }
 
    
//exec
    else if(function_exists('exec'))
    {
        exec($command , $output , $return_var);
        $output = implode("n" , $output);
    }
 
    
//shell_exec
    else if(function_exists('shell_exec'))
    {
        $output = shell_exec($command) ;
    }
 
    else
    {
        $output = 'Command execution not possible on this system';
        $return_var = 1;
    }
 
    return array('output' => $output , 'status' => $return_var);
}
terminal('ls');

 

posted @ 2016-03-25 09:40  天道酬勤,坚持!  阅读(257)  评论(0编辑  收藏  举报