PHP what is the best way to write data to middle of file without rewriting file

后端 未结 3 822
说谎
说谎 2020-11-29 10:31

I am working with large text files in php (1GB+), I am using

file_get_contents(\"file.txt\", NULL, NULL, 100000000,100); 

To get data from

相关标签:
3条回答
  • 2020-11-29 11:10

    To Overwrite Data :

    $fp = fopen("file.txt", "rw+");
    fseek($fp, 100000000); // move to the position
    fwrite($fp, $string, 100); // Overwrite the data in this position 
    fclose($fp);
    

    To Inject Data

    This is a tricky because you have to rewrite the file. It can be optimized with partial modificationfrom point of injection rather than the whole file

    $string = "###INJECT THIS DATA ##### \n";
    injectData("file.txt", $string, 100000000);
    

    Function Used

    function injectData($file, $data, $position) {
        $fpFile = fopen($file, "rw+");
        $fpTemp = fopen('php://temp', "rw+");
    
        $len = stream_copy_to_stream($fpFile, $fpTemp); // make a copy
    
        fseek($fpFile, $position); // move to the position
        fseek($fpTemp, $position); // move to the position
    
        fwrite($fpFile, $data); // Add the data
    
        stream_copy_to_stream($fpTemp, $fpFile); // @Jack
    
        fclose($fpFile); // close file
        fclose($fpTemp); // close tmp
    }
    
    0 讨论(0)
  • 2020-11-29 11:16

    A variant on Baba's answer, not sure if it would be more efficient when working with larger files:

    function injectData($file, $data, $position) {
        $fpFile = fopen($file, "rw+");
        $fpTemp = fopen('php://temp', "rw+");
        stream_copy_to_stream($fpFile, $fpTemp, $position);
        fwrite($fpTemp, $data);
        stream_copy_to_stream($fpFile, $fpTemp, -1, $position);
    
        rewind($fpFile);
        rewind($fpTemp);
        stream_copy_to_stream($fpTemp, $fpFile);
    
        fclose($fpFile);
        fclose($fpTemp);
    }
    
    injectData('testFile.txt', 'JKL', 3);
    

    Variant of my earlier method that eliminates one of the stream_copy_to_stream() calls, so should be a shade faster:

    function injectData3($file, $data, $position) {
        $fpFile = fopen($file, "rw+");
        $fpTemp = fopen('php://temp', "rw+");
        stream_copy_to_stream($fpFile, $fpTemp, -1, $position);
        fseek($fpFile, $position);
        fwrite($fpFile, $data);
        rewind($fpTemp);
        stream_copy_to_stream($fpTemp, $fpFile);
    
        fclose($fpFile);
        fclose($fpTemp);
    }
    
    0 讨论(0)
  • 2020-11-29 11:27

    Another variant of the injectData() function:

    function injectData($file, $data, $position) 
    {
        $temp = fopen('php://temp', "rw+");
        $fd = fopen($file, 'r+b');
    
        fseek($fd, $position);
        stream_copy_to_stream($fd, $temp); // copy end
    
        fseek($fd, $position); // seek back
        fwrite($fd, $data); // write data
    
        rewind($temp);
        stream_copy_to_stream($temp, $fd); // stich end on again
    
        fclose($temp);
        fclose($fd);
    }
    

    It copies the end of file (from $position onwards) into a temporary file, seeks back to write the data and stitches everything back up.

    0 讨论(0)
提交回复
热议问题