PHP在没有重写文件的情况下,将数据写入文件中间的最佳方式是什么? [英] PHP what is the best way to write data to middle of file without rewriting file

查看:229
本文介绍了PHP在没有重写文件的情况下,将数据写入文件中间的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用php(1GB +)中的大文本文件,我正在使用

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

要从文件中间获取数据,但如果我想更改文件中的数据对于与原始数据不同的变化,我将不得不重写整个文件。



如何更改文件中的数据(可变长度)如果数据大于原始数据,则不覆盖数据?我保留文件中不同数据块的索引及其字节位置。似乎唯一的选择是将x个字节数量专用于每个数据,然后重写该块,如果我想改变它...这个问题是,它将占用比只需要更多的空间空字节,这需要更长的时间写...而且仍然不会解决如何删除数据,因为文件不能缩小的大小...我真的需要一些帮助在这里...



如果我为文件中的每条数据使用了前缀块,如1 mb,那么我想输入只有 100kb ,该条目将需要10倍实际所需的空间,并且条目永远不会更改为超过1mb的数据,因为它会覆盖超过第一个专用块...删除它是不可能的...希望这个没有任何意义...我不寻找替代品,我正在寻找在文件中间写入和更改数据,呵呵...



更新:是的,我想替换旧的数据,但如果新的数据ext结束比旧数据我想要剩下的数据进一步推入文件...



考虑这个:0000000HELLODATA00000000
零代表空的空间,没有什么...现在我想用SOMETHING替换HELLO,现在由于某些东西比hello更大,只需在hello的起始位置编写就可以延伸一遍hello并开始覆盖数据...所以我想要DATA被推入文件,为SOMETHING腾出空间而不覆盖DATA ... hehe

解决方案

覆盖数据:

  $ fp = fopen(file.txt,rw +); 
fseek($ fp,100000000); //移动到位置
fwrite($ fp,$ string,100); //覆盖此位置的数据
fclose($ fp);

输入数据



这是一个棘手的问题,因为你必须重写该文件。可以从部分修改注入点而不是整个文件

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

使用功能



函数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); //移动到位置
fseek($ fpTemp,$ position); //移动到位置

fwrite($ fpFile,$ data); //添加数据

stream_copy_to_stream($ fpTemp,$ fpFile); // @Jack

fclose($ fpFile); //关闭文件
fclose($ fpTemp); //关闭tmp
}


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 the middle of the file, but if i wanted to change the data in the file to something that is of different change than the origional data, I would have to re-write the entire file.

How can I change data within the file (variable length) without overwriting data if the data is larger than the original? I keep an index of the different data blocks within the file and their byte location. It seems that the only alternative is to dedicate x amount of bytes to each piece of data and then rewrite that block if i wanted to change it... the problem with this is that it would take up a lot more space than needed in just null bytes, and it would take longer to write... and that still would not solve how to "remove" data, as the file could never shrink in size... I really need some help here...

If I used prefixed blocks for each piece of data in the file, like 1 mb, then I wanted to enter data that was only 100kb, that entry would take 10x actual needed space, and the entry could never be changed to something more than 1mb of data, as it would overwrite more than 1st dedicated block... removing it would not be possible... hope this makes any sense... I am not looking for alternatives, I am looking to write and change data in the middle of files, hehe...

UPDATE: Yes, I would like to replace the old data, but if the new data extends more than the old data I would want the rest of the data to be pushed further into the file...

consider this: 0000000HELLODATA00000000 the zeros represent empty space, nothing... now I would like to replace HELLO with SOMETHING, now since something is larger than hello, simply writing in the starting point of hello would extend byond hello and start overwriting data... therefore i would like DATA to be pushed futher into the file, to make room for SOMETHING without overwriting DATA... hehe

解决方案

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
}

这篇关于PHP在没有重写文件的情况下,将数据写入文件中间的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆