Using php, how to insert text without overwriting to the beginning of a text file

前端 未结 4 1409
灰色年华
灰色年华 2020-12-06 03:05

I have:

 \"\")
{
   fwrite($file,         


        
相关标签:
4条回答
  • 2020-12-06 03:17

    I'm not entirely sure of your question - do you want to write data and not have it over-write the beginning of an existing file, or write new data to the start of an existing file, keeping the existing content after it?

    To insert text without over-writing the beginning of the file, you'll have to open it for appending (a+ rather than r+)

    $file=fopen(date("Y-m-d").".txt","a+") or exit("Unable to open file!");
    
    if ($_POST["lastname"] <> "")
    {
       fwrite($file,$_POST["lastname"]."\n");
    }
    
    fclose($file);
    

    If you're trying to write to the start of the file, you'll have to read in the file contents (see file_get_contents) first, then write your new string followed by file contents to the output file.

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

    The above approach will work with small files, but you may run into memory limits trying to read a large file in using file_get_conents. In this case, consider using rewind($file), which sets the file position indicator for handle to the beginning of the file stream. Note when using rewind(), not to open the file with the a (or a+) options, as:

    If you have opened the file in append ("a" or "a+") mode, any data you write to the file will always be appended, regardless of the file position.

    0 讨论(0)
  • 2020-12-06 03:18

    A working example for inserting in the middle of a file stream without overwriting, and without having to load the whole thing into a variable/memory:

    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 for it can be found here

    0 讨论(0)
  • 2020-12-06 03:21

    If you want to put your text at the beginning of the file, you'd have to read the file contents first like:

    <?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);
    
    ?>
    
    0 讨论(0)
  • 2020-12-06 03:36

    You get the same opening the file for appending

    <?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);
    ?>
    
    0 讨论(0)
提交回复
热议问题