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

如何在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条)

报告相同问题?

悬赏问题

  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥200 求能开发抖音自动回复卡片的软件
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。