Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear memory after file_get_contents executes in php

Tags:

php

php-5.5

I am trying to add certain variables to couple of files which already have some content.

I am using file_get_contents to copy the contents of a particular file and then using file_put_contents to paste variable values along with the existing contents to that file.

The problem is that, on the first instance it works properly but to the second file it pastes everything that has been stored in the memory. It puts all the contents from the first file along with the contents of the second file.

Is there any way that I can clear the memory before the next file_get_contents executes. Or my concept is false here.

Here is my code...

<?php

 if ($_POST["submit"]) {

    $ip = $_POST['ip'];
    $subnet = $_POST['subnet'];
    $gateway = $_POST['gateway'];
    $hostname = $_POST['hostname'];
    $domain = $_POST['domain'];
    $netbios = $_POST['netbios'];
    $password = $_POST['password'];


    $ipfile = 'one.txt';

    $file = fopen($ipfile, "r");
    $ipfileContents = fread($file, filesize($ipfile));

    $ipcontent = "ip='$ip'\n";
    $ipcontent .= "netmask='$subnet'\n";
    $ipcontent .= "gw='$gateway'\n";
    $conten = $ipcontent . $ipfileContents;

    $file = fopen($ipfile, "w");
    fwrite($file, $ipfileContents);

    fclose($file);

    $ipsh = shell_exec('sh path/to/CHANGE_IP.sh');



    $hostfile = 'two.txt';

    $fileh = fopen($hostfile, "r");
    $hostfileContents = fread($fileh, filesize($hostfile));

    $hostcontent = "ip='$ip'\n";
    $hostcontent .= "m_name='$hostname'\n";
    $hostcontent .= "fqdn='$domain'\n";
    $conten = $hostcontent . $hostfileContents;

    $fileh = fopen($hostfile, "w");
    fwrite($fileh, $hostfileContents);

    fclose($fileh);

$hostsh = shell_exec('sh path/to/MODIFY_HOSTS.sh');


}








?>

I have tried unset, but didn't work

$ipfilecontents->__destruct();
unset($ipfilecontents);

UPDATE:

file_get_contents & file_put_contents has some concurrency problems. So I had to change my method to fopen/fwrite/fclose and it worked flawlessly. Thanks for your help Jacinto.

like image 838
PeeJay Avatar asked Nov 21 '25 20:11

PeeJay


1 Answers

        if ($_POST["submit"]) {

        $ip = $_POST['ip'];
        $subnet = $_POST['subnet'];
        $gateway = $_POST['gateway'];
        $hostname = $_POST['hostname'];
        $domain = $_POST['domain'];
        $netbios = $_POST['netbios'];
        $password = $_POST['password'];


        $ipfile = 'one.txt';

        $file = fopen($ipfile, "r");
        $ipfileContents = fread($file, filesize($ipfile));

        $ipcontent = "ip='$ip'\n";
        $ipcontent .= "netmask='$subnet'\n";
        $ipcontent .= "gw='$gateway'\n";
        $content = $ipcontent . $ipfileContents;

        $file = fopen($ipfile, "w");
        fwrite($file, $content);

        fclose($file);

        $ipsh = shell_exec('sh path/to/CHANGE_IP.sh');

//do the same to the next file
}
like image 152
Jacinto Avatar answered Nov 23 '25 10:11

Jacinto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!