如何通过php中的系统命令替换exec()?

时间:2011-09-27 05:19:21

标签: php

我的编码到目前为止:

如何通过php中的系统命令替换exec()

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);

1 个答案:

答案 0 :(得分:2)

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

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

hostname返回的内容应该使用$_SERVER['SERVER_NAME']代替。