使用PHP编辑PHP文件,不含重复内容

时间:2015-04-07 03:26:19

标签: php copy file-get-contents file-put-contents

所以我一直在尝试编辑PHP文件后,我似乎一直遇到问题。

这是我的源代码。

if($pbtype == 'pwall' OR $pbtype == 'cl')
{
    $file = '../postback/pwallpb.php';
    $pbac = '../postback/'.md5($affn).'.php';
}
else
{
    $file = '../postback/postback.php';
    $pbac = '../postback/'.md5($affn).'.php';
}

copy($file, $pbac);
// Read the while file into a string $htaccess
$files = file_get_contents($pbac);

// Stick the new IP just before the closing </files>
$new_files  = str_replace('//NET NAME//', "$affn", $files);
$new_files .= str_replace('// IP 1 //', "$affip", $files);
$new_files .= str_replace('// IP 2 //', "$affip2", $files);
$new_files .= str_replace('// IP 3 //', "$affip3", $files);
$new_files .= str_replace('// IP 4 //', "$affip4", $files);

// And write the new string back to the file
file_put_contents($pbac, $new_files);
$pback = '/postback/'.md5($affn).'.php';

它会创建文件。它不是一个预先设置的问题,它也编辑文件,但每次我做一个字符串替换时它会生成php,所以id我str_replace 5次然后文件中有5个代码块实例。我做错了什么?

更新我的代码。这次我做错了什么?

if($pbtype == 'pwall' OR $pbtype == 'cl')
{
    $file = '../postback/pwallpb.php';
    $pbac = '../postback/'.md5($affn).'.php';
}
else
{
    $file = '../postback/postback.php';
    $pbac = '../postback/'.md5($affn).'.php';
}

$myfile = fopen($file, "r") or die("Unable to open file!");
$files = fread($myfile,filesize($file));
fclose($myfile);


// Stick the new IP just before the closing </files>
$new_files = str_replace('||NETNAME||', $affn, $files);
$new_files = str_replace('||IP1||', $affip, $files);
$new_files = str_replace('||IP2||', $affip2, $files);
$new_files = str_replace('||IP3||', $affip3, $files);
$new_files = str_replace('||IP4||', $affip4, $files);

// And write the new string back to the file
$fh = fopen($pbac, 'w') or die("can't open file");
$stringData = $new_files;
fwrite($fh, $stringData);
fclose($fh);

$pback = '/postback/'.md5($affn).'.php';

2 个答案:

答案 0 :(得分:0)

这有效并经过测试:

    error_reporting(-1);
    ini_set('display_errors', 'On');



    if($pbtype == 'pwall' OR $pbtype == 'cl')
    {
        $file = 'pwallpb.php';
        $pbac = md5($affn).'.php';
    }
    else
    {
        $file = 'postback.php';
        $pbac = md5($affn).'.php';
    }


    $myfile = fopen('../postback/' . $file, "r") or die("Unable to open file!");
    $files = fread($myfile,filesize($pbac));
    fclose($myfile);


    // Stick the new IP just before the closing </files>
    $new_files  = str_replace('||NetName||', $affn, $files);
    $new_files = str_replace('||IP1||', $affip, $new_files);
    $new_files = str_replace('||IP2||', $affip2, $new_files);
    $new_files = str_replace('||IP3||', $affip3, $new_files);
    $new_files = str_replace('||IP4||', $affip4, $new_files);



    $myFile = md5($affn).'.php';
    $fh = fopen('../postback/' . $myFile, 'w') or die("can't open file");
    $stringData = $new_files;
    fwrite($fh, $stringData);
    fclose($fh);

    $pback = '../postback/'.md5($affn).'.php';

测试文件呈现:

$IParray=array("1.2.3.4","2.3.4.5","4.5.6.7","5.6.7.8");

答案 1 :(得分:0)

您正在使用“。=”而不是“=”。 “。=”将结果附加到文件中。您的代码可能看起来更像这样:

if($pbtype == 'pwall' OR $pbtype == 'cl')
  {
  $file = '../postback/pwallpb.php';
  }
else
  {
  $file = '../postback/postback.php';
  }
$pbac = '../postback/'.md5($affn).'.php';

copy($file, $pbac);

// Read the while file into a string $htaccess
$files = file_get_contents($pbac);

// Stick the new IP just before the closing </files>
$searchArray = array('//NET NAME//','// IP 1 //','// IP 2 //','// IP 3 //','// IP 4 //');
$replaceArray = array("$affn","$affip","$affip2","$affip3","$affip4");
$new_files  = str_replace($searchArray,$replaceArray,$files);

// And write the new string back to the file
file_put_contents($pbac, $new_files);
$pback = '/postback/'.md5($affn).'.php';