我的当前代码:
$fileone= fopen("fileone.sql", "r"); //opens file fileone.sql
$fileonewrite = fopen("fileone.rdf", "w"); //this is the file to write to
$fileNum=1;
$i=0;
while (!feof($fileone) ) { //feof = while not end of file
if ($contents = fread($fileonewrite ,53687091200)); //if file is more than 50gb, write to new file (below) .. this doesnt seem to work properly
{ file_put_contents('fileone'.$fileNum.'.rdf',$contents);
$fileNum++;
}
$fileoneRow[] = fgets($fileone); //fgets gets line
$fileoneParts = explode("'t", $fileoneRow[$i]); //explode using tab delimiter
fwrite( " lots of stuff" );
unset($fileoneParts);
}
fclose($fileonetype);
fclose($fileonewrite);
我读取了大量数据并输出了更多数据,创建的文件很容易达到200GB以上。这会导致内存问题。所以我想做的是,当正在写入的文件,例如fileone.rdf,达到50gb时,我想开始写入filetwo。我的代码atm工作不太好,因为它似乎输出了数千个空文件。
感谢您阅读我的查询,如有任何帮助,我们将一如既往地不胜感激。
if ($contents = fread($fileonewrite ,53687091200)); //if file is more than 50gb, write to new file (below) .. this doesnt seem to work properly
^----BUG BUG BUG
{ file_put_contents('fileone'.$fileNum.'.rdf',$contents);
$fileNum++;
}
;
在那里终止了if()语句,因此{}
内部的代码是if()的NOT部分;如果fread没有返回任何数据(eof),那么无论If()测试是如何产生的,您仍然会从文件中写入一个空的$contents。
一口气读取50吉的数据简直是疯了。为什么不做更多的事情:
$in = fopen('input');
$out = fopen('output');
$read = 0;
while($data = fread($fh, 1024*1024)) { // read in 1meg chunks
fwrite($out, $data);
$read += strlen($data);
if ($read > 50gig) {
fclose($out);
$out = fopen('new file goes here');
$read = 0;
}
}
它将以1meg的块进行复制,这将大大减轻系统内存的压力,然后在最终达到50gig复制时切换到新文件。