2011-09-27 45 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