我有一个197 to文本文件,我想读取该文本文件并将内容推入MySql数据库。我知道,我不能把那个大文件放在PHP缓冲区中并作为一个整体来读取,所以我想作为一次读取几百行,然后继续读取整个文件。
我正在尝试这个,但是页面没有返回任何内容。
<?php
$i = 0;
$handle = fopen("./data/200gbfile.txt", "r") or die("Couldn't get handle");
if ($handle) {
while (($line = fgets($handle)) !== false) {
echo $line . "<br />";
if ($i > 100) {
exit;
}
$i++;
}
fclose($handle);
} else {
echo "Error Opeing File!";
}
?>在php设置中要处理的最大文件大小是否有限制?
编辑:对于问题中的197gb文件,fopen没有返回任何内容,输出页只是变为空白。
发布于 2017-11-25 21:40:49
您可以以块的形式读取文件以节省内存:
例如:
$fd = @fopen("./data/200gbfile.txt", "r");
while (!feof($fd)) {
$data = fread($fd, 1024); // read the file in 1024kb chunks
// handle current data (read line by line for example)
}
fclose($fd);但不知道这是否适用于100Gbytes+的文件。
Edit: @ with fopen is required as suggested by Roman.发布于 2017-11-25 21:36:31
您可以使用ini_set('memory_limit','16M');相应地设置大小,但我不知道它是否会处理这么大的文件。从未测试过..。
https://stackoverflow.com/questions/47490719
复制相似问题