我有:

<?php

$file=fopen(date("Y-m-d").".txt","r+") or exit("Unable to open file!");

if (

我有:

<*>

但它会覆盖文件的开头。如何插入?

POST["lastname"] <> "") { fwrite($file,

我有:

<*>

但它会覆盖文件的开头。如何插入?

POST["lastname"]."\n"); } fclose($file); ?>

但它会覆盖文件的开头。如何插入?

有帮助吗?

解决方案

我不完全确定你的问题 - 你想写数据而不是覆盖现有文件的开头,或者将新数据写入现有文件的开头,保留现有内容它?

要插入文字而不覆盖文件的开头,您必须打开它才能追加( a + 而不是 r +

$file=fopen(date("Y-m-d").".txt","a+") or exit("Unable to open file!");

if (

我不完全确定你的问题 - 你想写数据而不是覆盖现有文件的开头,或者将新数据写入现有文件的开头,保留现有内容它?

要插入文字而不覆盖文件的开头,您必须打开它才能追加( a + 而不是 r +

$old_content = file_get_contents($file);
fwrite($file, $new_content."\n".$old_content);

如果您尝试写入文件的开头,则必须阅读文件内容(请参阅 <代码>的file_get_contents )首先,然后将新字符串后跟文件内容写入输出文件。

<*>

上述方法适用于小文件,但是您可能会遇到内存限制,尝试使用 file_get_conents 读取大文件。在这种情况下,请考虑使用 rewind($ file) 来设置文件句柄的位置指示器到文件流的开头。 使用 rewind()时请注意,不要使用 a (或 a + )选项打开文件,如下所示:

  

如果您已在附加(“a”或“a +”)模式下打开文件,则无论文件位置如何,都会追加您写入文件的所有数据。

POST["lastname"] <> "") { fwrite($file,

我不完全确定你的问题 - 你想写数据而不是覆盖现有文件的开头,或者将新数据写入现有文件的开头,保留现有内容它?

要插入文字而不覆盖文件的开头,您必须打开它才能追加( a + 而不是 r +

<*>

如果您尝试写入文件的开头,则必须阅读文件内容(请参阅 <代码>的file_get_contents )首先,然后将新字符串后跟文件内容写入输出文件。

<*>

上述方法适用于小文件,但是您可能会遇到内存限制,尝试使用 file_get_conents 读取大文件。在这种情况下,请考虑使用 rewind($ file) 来设置文件句柄的位置指示器到文件流的开头。 使用 rewind()时请注意,不要使用 a (或 a + )选项打开文件,如下所示:

  

如果您已在附加(“a”或“a +”)模式下打开文件,则无论文件位置如何,都会追加您写入文件的所有数据。

POST["lastname"]."\n"); } fclose($file);

如果您尝试写入文件的开头,则必须阅读文件内容(请参阅 <代码>的file_get_contents )首先,然后将新字符串后跟文件内容写入输出文件。

<*>

上述方法适用于小文件,但是您可能会遇到内存限制,尝试使用 file_get_conents 读取大文件。在这种情况下,请考虑使用 rewind($ file) 来设置文件句柄的位置指示器到文件流的开头。 使用 rewind()时请注意,不要使用 a (或 a + )选项打开文件,如下所示:

  

如果您已在附加(“a”或“a +”)模式下打开文件,则无论文件位置如何,都会追加您写入文件的所有数据。

其他提示

一个工作示例,用于在不覆盖的情况下插入文件流的中间,而无需将整个内容加载到变量/内存中:

function finsert($handle, $string, $bufferSize = 16384) {
    $insertionPoint = ftell($handle);

    // Create a temp file to stream into
    $tempPath = tempnam(sys_get_temp_dir(), "file-chainer");
    $lastPartHandle = fopen($tempPath, "w+");

    // Read in everything from the insertion point and forward
    while (!feof($handle)) {
        fwrite($lastPartHandle, fread($handle, $bufferSize), $bufferSize);
    }

    // Rewind to the insertion point
    fseek($handle, $insertionPoint);

    // Rewind the temporary stream
    rewind($lastPartHandle);

    // Write back everything starting with the string to insert
    fwrite($handle, $string);
    while (!feof($lastPartHandle)) {
        fwrite($handle, fread($lastPartHandle, $bufferSize), $bufferSize);
    }

    // Close the last part handle and delete it
    fclose($lastPartHandle);
    unlink($tempPath);

    // Re-set pointer
    fseek($handle, $insertionPoint + strlen($string));
}

$handle = fopen("file.txt", "w+");
fwrite($handle, "foobar");
rewind($handle);
finsert($handle, "baz");

// File stream is now: bazfoobar

可以在此处找到Composer lib

你打开文件以便附加

<?php
$file=fopen(date("Y-m-d").".txt","a+") or exit("Unable to open file!");
if (

你打开文件以便附加

<*>POST["lastname"] <> "") { fwrite($file,

你打开文件以便附加

<*>POST["lastname"]."\n"); } fclose($file); ?>

如果您想将文本放在文件的开头,则必须首先阅读文件内容,如:

<?php

$file=fopen(date("Y-m-d").".txt","r+") or exit("Unable to open file!");

if (

如果您想将文本放在文件的开头,则必须首先阅读文件内容,如:

<*>POST["lastname"] <> "") { $existingText = file_get_contents($file); fwrite($file, $existingText .

如果您想将文本放在文件的开头,则必须首先阅读文件内容,如:

<*>POST["lastname"]."\n"); } fclose($file); ?>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top