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

处理大文件时没有经验,所以我不知道该怎么做.我试图使用file_get_contents读取几个大文件;任务是使用preg_replace()来清理它们并使其成群.

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

PHP Fatal error:  Allowed memory size of 16777216 bytes exhausted (tried to allocate 41390283 bytes)

我正在考虑使用fread(),但我不知道这也会工作.这个问题有解决方法吗?

感谢您的输入.

这是我的代码

<?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,$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,$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,$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]+/",$string);
}
?>
首先,您应该明白,使用file_get_contents时,将整个数据字符串提取到变量中,该变量存储在主机内存中.

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

围绕这个方式打开文件作为一个指针,然后一个块一次,这样如果你有一个500MB的文件,你可以读取第一个1MB的数据,做你会用它,删除1MB从系统的内存和替换下一个MB,这允许您管理在内存中放入多少数据.

一个例子,如果这可以在下面看到,我将创建一个像node.js一样的功能

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;
}

然后使用如下:

$success = file_get_contents_chunked("my/large/file",4096,function($chunk,$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 fo the file that has been read so
        * ($i * 4096) is your current offset within the file.
    */

});

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

您会发现的一个问题是,您尝试在极大的数据量上执行正则表达式,不仅仅是正则表达式,是为了匹配整个文件而构建的.

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

> strpos
> substr
>修剪
>爆炸

为了匹配字符串,我在回调中添加支持,以便传递句柄和当前迭代,这将允许您直接在回调中使用该文件,从而允许您使用fseek,ftruncate和fwrite等函数.

建立你的字符串操作的方式并不是有效的,所以使用上面提出的方法一个更好的方法.

希望这可以帮助.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...