编辑 PHP 文件 PHP 没有重复的内容

Edit a PHP file with PHP without duplicate content

所以我一直在尝试编辑一个 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';

这有效并经过测试:

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

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

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