2011-09-27 126 views
0

我的编码是迄今为止:如何通过系统命令在php中替换exec()?

如何通过系统命令替换exec()在PHP?

if($str!="success") 
{ 
    $cmd = "rm -rf /portal/data/config/certificate/tmp/"; 
    $error_text="Command : ".$cmd; 
    AddLog("sslconfig.php",$error_text,ERR_DEBUG_HIGH); 
    $output = exec($cmd,$array1,$error_code); 
    $error_text="Error code : ".$error_code; 
    AddLog("sslconfig.php",$error_text,ERR_DEBUG_HIGH); 
    seterror('0:|: :|: Could not apply certificate.'); 
    $error_text="Could not apply certificate"; 
    AddLog("sslconfig.php",$error_text,ERR_INFO); 
    header("Location: ssl.php"); 
    exit; 
} 

if($certName==$cert_auth) 
{ 
    //copy the applied certificate to fireballbundle.crt 
    //$output = copysslfb("/portal/data/config/certificate/".$newfile.".crt"); 
    $error_text="Selfsigned Certicate"; 
    AddLog("sslconfig.php",$error_text,ERR_DEBUG_HIGH); 
    $output="success"; 
} 
else 
{ 
    $error_text="Not Selfsigned Certicate"; 
    AddLog("sslconfig.php",$error_text,ERR_DEBUG_HIGH); 
    $output="success"; 
} 

if($output!="success") 
{ 
    $cmd = "rm -rf /portal/data/config/certificate/tmp/"; 
    $error_text="Command : ".$cmd; 
    AddLog("sslconfig.php",$error_text,ERR_DEBUG_HIGH); 
    $output = exec($cmd,$array1,$error_code); 
    $error_text="Error code : ".$error_code; 
    AddLog("sslconfig.php",$error_text,ERR_DEBUG_HIGH); 
    $error_text="Could not add certificate to fireballbundle.crt : ".$output; 
    AddLog("sslconfig.php",$error_text,ERR_ERROR); 
    seterror('0:|: :|: Error in applying certificate.'); 
    header("Location: ssl.php"); 
    exit; 
} 

现在我想用系统命令替换exec命令吗?

我使用三次exec()这里显示为上面的代码现在我想在PHP与system()命令替换

exec("hostname",$retval); 
$output = exec($cmd,$array1,$error_code); 
exec($cmd,$array1,$error_code); 

回答

2

要删除一个文件,你应该使用unlink和删除目录,你应该使用rmdir。在这些页面的评论中,你会发现许多不同的方式来通过这些函数来模拟rm -rf

您应该尽可能避免使用systemexec。总是看看php.net和谷歌,看看你是否可以找到一种方法来做任何你想要做的事情与一个内置的函数或库。您无需在这里使用这些设施。

什么hostname返回您应该可能使用$_SERVER['SERVER_NAME']代替。

+0

:)这不是我的答 – John

+0

:)我也在使用unlink编码 – John

相关问题