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 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3