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

时间:2015-03-20 20:26:24

标签: php php-5.5

我正在尝试将某些变量添加到已经有一些内容的几个文件中。

我使用file_get_contents复制特定文件的内容,然后使用file_put_contents将变量值和现有内容粘贴到该文件中。

问题是,在第一个实例上它可以正常工作,但是对于第二个文件,它会粘贴存储在内存中的所有内容。它将第一个文件中的所有内容与第二个文件的内容一起放入。

有没有办法在下一个file_get_contents执行之前清除内存。或者我的概念在这里是假的。

这是我的代码......

<?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');


}








?>

我试过未设置,但没有工作

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

更新

file_get_contents&amp; file_put_contents有一些并发问题。所以我不得不将我的方法改为fopen/fwrite/fclose,它完美无瑕。谢谢你的帮助Jacinto。

3 个答案:

答案 0 :(得分:3)

        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
}

答案 1 :(得分:2)

这不是答案 - 我会在一分钟后将其删除。这是展示如何进行跟踪语句的一个方便的地方:

    $ipfile = 'one.txt';
    $ipfileContents = file_get_contents($ipfile);
    $ipcontent = "ip='$ip'\n";
    $ipcontent .= "netmask='$subnet'\n";
    $ipcontent .= "gw='$gateway'\n";
    echo "DEBUG: hostcontent=<pre>$ipcontent</pre><br />====<br />hostfileContents=<pre>$ipfileContents</pre><br />\n";            
    file_put_contents($ipfile, $ipcontent . $ipfileContents,  LOCK_EX);
    $ipsh = shell_exec('sh path/to/CHANGE_IP.sh');

    $hostfile = 'two.txt';
    $hostfileContents = file_get_contents($hostfile);
    $hostcontent = "ip='$ip'\n";
    $hostcontent .= "m_name='$hostname'\n";
    $hostcontent .= "fqdn='$domain'\n";
    echo "DEBUG: hostcontent=<pre>$hostcontent</pre><br />====<br />hostfileContents=<pre>$hostfileContents</pre><br />\n";
    file_put_contents($hostfile, $hostcontent . $hostfileContents,  LOCK_EX);
    $hostsh = shell_exec('sh path/to/MODIFY_HOSTS.sh');

答案 2 :(得分:0)

最有效的方法是同时以块的形式读取和写入文件:

  • 以读写模式打开文件
  • 读取将被新数据覆盖的数据块
  • 将指针重置为读取块的开始
  • 撰写新数据
  • 使读取数据成为要写入的新数据
  • 重复,直到没有要写的数据

示例:

$bufw = "ip=''\n";
$bufw .= "netmask=''\n";
$bufw .= "gw=''\n";

$bufw_len = strlen($bufw);

$file = fopen($ipfile, 'c+');
while ($bufw_len > 0) {
    // read next chunk
    $bufr = fread($file, $bufw_len);
    $bufr_len = strlen($bufr);

    // reset pointer to begin of chunk
    fseek($file, -$bufr_len, SEEK_CUR);

    // write previous chunk
    fwrite($file, $bufw);

    // update variables
    $bufw = $bufr;
    $bufw_len = strlen($bufw);
}
fclose($file);

这样,总内存使用量只能达到要写入的新数据的长度。

你可以使它成为像这样的函数:

function file_prepend_contents($filename, $data, $flags = 0, $context = null) {
    if (!is_null($context)) {
        $handle = fopen($filename, 'c+', ($flags & FILE_USE_INCLUDE_PATH) === FILE_USE_INCLUDE_PATH, $context);
    } else {
        $handle = fopen($filename, 'c+', ($flags & FILE_USE_INCLUDE_PATH) === FILE_USE_INCLUDE_PATH);
    }

    if (!$handle) return false;

    if (($flags & LOCK_EX) === LOCK_EX) {
        flock($handle, LOCK_EX);
    }

    $bytes_written = 0;

    $bufw = $data;
    $bufw_len = strlen($bufw);
    while ($bufw_len > 0) {
        // read next chunk
        $bufr = fread($handle, $bufw_len);
        $bufr_len = strlen($bufr);

        // reset pointer
        fseek($handle, -$bufr_len, SEEK_CUR);

        // write current chunk
        if (ftell($handle) === 0) {
            $bytes_written = fwrite($handle, $bufw);
        } else {
            fwrite($handle, $bufw);
        }

        // update variables
        $bufw = $bufr;
        $bufw_len = strlen($bufw);
    }
    fclose($handle);

    return $bytes_written;
}