小编use*_*085的帖子

从大文件php的中间删除一行

我试图从大文件的中间删除一行.(> 20MB).我知道要删除的行开头的文件中的位置.

这是我现在拥有的.

/**
 * Removes a line at a position from the file
 * @param  [int] $position  The position at the start of the line to be removed
 */
public function removeLineAt($position)
{
    $fp = fopen($this->filepath, "rw+");
    fseek($fp, $position);

    $nextLinePosition = $this->getNextLine($position, $fp);
    $lengthRemoved = $position - $nextLinePosition;
    $fpTemp = fopen('php://temp', "rw+");

    // Copy the bottom half (starting at line below the line to be removed)
    stream_copy_to_stream($fp, $fpTemp, -1, $nextLinePosition);

    // Seek to the start of the line to …
Run Code Online (Sandbox Code Playgroud)

php file

5
推荐指数
1
解决办法
454
查看次数

标签 统计

file ×1

php ×1