首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >file_get_contents => PHP致命错误:允许的内存耗尽

file_get_contents => PHP致命错误:允许的内存耗尽
EN

Stack Overflow用户
提问于 2011-03-10 00:56:44
回答 4查看 59.7K关注 0票数 31

我没有处理大文件的经验,所以我不知道该怎么做。我尝试过使用file_get_contents读取几个大文件;我的任务是使用preg_replace().清理和转换它们

我的代码在小文件上运行良好;但是,大文件(40MB)会触发内存耗尽错误:

代码语言:javascript
复制
PHP Fatal error:  Allowed memory size of 16777216 bytes exhausted (tried to allocate 41390283 bytes)

我正在考虑使用fread()来代替,但我也不确定这是否可行。有没有解决这个问题的办法?

感谢您的意见。

这是我的代码:

代码语言:javascript
复制
<?php
error_reporting(E_ALL);

##get find() results and remove DOS carriage returns.
##The error is thrown on the next line for large files!
$myData = file_get_contents("tmp11");
$newData = str_replace("^M", "", $myData);

##cleanup Model-Manufacturer field.
$pattern = '/(Model-Manufacturer:)(\n)(\w+)/i';
$replacement = '$1$3';
$newData = preg_replace($pattern, $replacement, $newData);

##cleanup Test_Version field and create comma delimited layout.
$pattern = '/(Test_Version=)(\d).(\d).(\d)(\n+)/';
$replacement = '$1$2.$3.$4      ';
$newData = preg_replace($pattern, $replacement, $newData);

##cleanup occasional empty Model-Manufacturer field.
$pattern = '/(Test_Version=)(\d).(\d).(\d)      (Test_Version=)/';
$replacement = '$1$2.$3.$4      Model-Manufacturer:N/A--$5';
$newData = preg_replace($pattern, $replacement, $newData);

##fix occasional Model-Manufacturer being incorrectly wrapped.
$newData = str_replace("--","\n",$newData);

##fix 'Binary file' message when find() utility cannot id file.
$pattern = '/(Binary file).*/';
$replacement = '';
$newData = preg_replace($pattern, $replacement, $newData);
$newData = removeEmptyLines($newData);

##replace colon with equal sign
$newData = str_replace("Model-Manufacturer:","Model-Manufacturer=",$newData);

##file stuff
$fh2 = fopen("tmp2","w");
fwrite($fh2, $newData);
fclose($fh2);

### Functions.

##Data cleanup
function removeEmptyLines($string)
{
        return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $string);
}
?>
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-03-10 01:48:01

首先,您应该了解,当使用file_get_contents时,您将整个数据字符串提取到一个变量中,该变量存储在主机内存中。

如果该字符串大于PHP进程专用的大小,则PHP将暂停并显示上面的错误消息。

解决这个问题的方法是将文件作为指针打开,然后一次获取一个块。这样,如果你有一个500MB的文件,你可以读取第一个1MB的数据,对它做任何你想做的事情,从系统内存中删除1MB,并用下一个MB替换它。这使您可以管理要放入内存中的数据量。

如果可以在下面看到一个示例,我将创建一个类似于node.js的函数

代码语言:javascript
复制
function file_get_contents_chunked($file,$chunk_size,$callback)
{
    try
    {
        $handle = fopen($file, "r");
        $i = 0;
        while (!feof($handle))
        {
            call_user_func_array($callback,array(fread($handle,$chunk_size),&$handle,$i));
            $i++;
        }

        fclose($handle);

    }
    catch(Exception $e)
    {
         trigger_error("file_get_contents_chunked::" . $e->getMessage(),E_USER_NOTICE);
         return false;
    }

    return true;
}

然后像这样使用:

代码语言:javascript
复制
$success = file_get_contents_chunked("my/large/file",4096,function($chunk,&$handle,$iteration){
    /*
        * Do what you will with the {$chunk} here
        * {$handle} is passed in case you want to seek
        ** to different parts of the file
        * {$iteration} is the section of the file that has been read so
        * ($i * 4096) is your current offset within the file.
    */

});

if(!$success)
{
    //It Failed
}

您将发现的问题之一是,您正在尝试对非常大的数据块执行多次正则表达式。不仅如此,您的正则表达式也是为匹配整个文件而构建的。

使用上面的方法,您的正则表达式可能会变得毫无用处,因为您可能只匹配了半组数据。您应该做的是恢复到本机字符串函数,例如

  • strpos
  • substr
  • trim
  • explode

为了匹配字符串,我在回调中添加了支持,以便传递句柄和当前迭代。这将允许您在回调中直接处理文件,例如,允许您使用fseekftruncatefwrite等函数。

构建字符串操作的方式无论如何都没有效率,使用上面提出的方法是一种好得多的方法。

希望这能有所帮助。

票数 103
EN

Stack Overflow用户

发布于 2011-03-10 01:00:37

根据文件大小调整内存限制的一个非常丑陋的解决方案:

代码语言:javascript
复制
$filename = "yourfile.txt";
ini_set ('memory_limit', filesize ($filename) + 4000000);
$contents = file_get_contents ($filename);

正确的解决方案应该是考虑是否可以将文件分成较小的块进行处理,或者使用PHP中的命令行工具。

如果您的文件是基于行的,您还可以使用fgets逐行处理它。

票数 3
EN

Stack Overflow用户

发布于 2020-07-24 00:18:01

为了一次只处理n行,我们可以在PHP中使用generators

N(使用1000)

这就是它的工作原理,读取n行,处理它们,返回到n+1,然后读取n行,处理它们,并读取下n行,依此类推。

下面是执行此操作的代码。

代码语言:javascript
复制
<?php
class readLargeCSV{

    public function __construct($filename, $delimiter = "\t"){
        $this->file = fopen($filename, 'r');
        $this->delimiter = $delimiter;
        $this->iterator = 0;
        $this->header = null;
    }

    public function csvToArray()
    {
        $data = array();
        while (($row = fgetcsv($this->file, 1000, $this->delimiter)) !== false)
        {
            $is_mul_1000 = false;
            if(!$this->header){
                $this->header = $row;
            }
            else{
                $this->iterator++;
                $data[] = array_combine($this->header, $row);
                if($this->iterator != 0 && $this->iterator % 1000 == 0){
                    $is_mul_1000 = true;
                    $chunk = $data;
                    $data = array();
                    yield $chunk;
                }
            }
        }
        fclose($this->file);
        if(!$is_mul_1000){
            yield $data;
        }
        return;
    }
}

为了阅读它,你可以使用这个。

代码语言:javascript
复制
    $file = database_path('path/to/csvfile/XYZ.csv');
    $csv_reader = new readLargeCSV($file, ",");


    foreach($csv_reader->csvToArray() as $data){
     // you can do whatever you want with the $data.
    }

这里,$data包含来自csv的1000个条目,或将用于最后一批的n%1000。

有关这方面的详细解释,请参阅此处https://medium.com/@aashish.gaba097/database-seeding-with-large-files-in-laravel-be5b2aceaa0b

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5249279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档