Search code examples
phpcopyfile-get-contentsfile-put-contents

Edit a PHP file with PHP without duplicate content


So I have been trying to edit a PHP file after it is created and I seem to keep running into problems.

Here is my source code.

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

it creates the file. it is not a premissions problem, it edits file as well, but it generates the php each time i do a string replace, So id I str_replace 5 times then there is 5 instances of the code blocks in the file. what is it that I am doing wrong?

update to my code. what did i do wrong this time?

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

Solution

  • This works and is tested:

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

    Test File renders this:

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