0

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';
c0d3x1337
  • 47
  • 11

2 Answers2

1

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");
MrTechie
  • 1,797
  • 4
  • 20
  • 36
0

You are using ".=" instead of "=". ".=" appends your results to the file. Your code might look better like this:

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';
John K
  • 55
  • 1
  • 3
  • That doesn't help any. Does same thing. didn't think it would help as that is how i originally had it, before i changed it, to how it is. – c0d3x1337 Apr 07 '15 at 04:07