dtc4547 2015-03-20 20:26
浏览 1101
已采纳

如何在php中执行file_get_contents后清除内存

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'
";
    $ipcontent .= "netmask='$subnet'
";
    $ipcontent .= "gw='$gateway'
";
    $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'
";
    $hostcontent .= "m_name='$hostname'
";
    $hostcontent .= "fqdn='$domain'
";
    $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.

  • 写回答

3条回答 默认 最新

  • duankui3838 2015-03-20 21:15
    关注
            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'
    ";
            $ipcontent .= "netmask='$subnet'
    ";
            $ipcontent .= "gw='$gateway'
    ";
            $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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。