xml_parse没有内存错误PHP [英] xml_parse No memory error PHP

查看:70
本文介绍了xml_parse没有内存错误PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用xml_parse时出现一个奇怪的错误.我的脚本通过xml_parse函数在XML文件的最后一行返回"No memory"错误.仅当文件大小大于10Mb时才会发生这种情况.更少是可以接受的.但是我有3Gb适用于PHP脚本,总内存为32Gb.

I've got a strange bug when using xml_parse. My script returns "No memory" error on last line of XML file by xml_parse function. This only happens when size of file bigger then 10Mb. Less is acceptable. But I have 3Gb avilable for PHP script and total memory is 32Gb.

该脚本曾经在另一台服务器(PHP为2Gb,总为16Gb)上运行时起作用,并且可以处理更大的文件.但是它是FreeBSD,现在是在CentOS 6.4下.

This script used to be working while it was working on another server (with 2Gb for PHP and 16Gb total) and it worked with even bigger files. But it was FreeBSD, now it is under CentOS 6.4.

也许有人有同样的情况吗?

May be somebody has same situation?

推荐答案

libxml"LIBXML_PARSEHUGE"中有一个硬编码的限制 检查 http://php.net/manual/en/libxml.constants.php详细信息.

There is a limit hardcoded in libxml "LIBXML_PARSEHUGE" Check http://php.net/manual/en/libxml.constants.php for details.

但是您不需要降级libxml.只需更改您调用xml_parse的方式即可.

But you don't need to downgrade libxml. Just change the way you call xml_parse.

例如,对于超过10MB的文件,这种方式不起作用:

For example, with a file which exceed 10MB, this way doesn't work :

$fileContent = file_get_contents("/tmp/myFile.xml");
if (!xml_parse($this->xmlParser, $fileContent, true))
{
    $xmlErreurString = xml_error_string(xml_get_error_code($xmlParser));
}

但是如果您以5MB的大小读取文件5,就可以了:

But if you read your file 5 by 5MB, it's ok :

$xmlParser = xml_parser_create();
$fp = fopen("/tmp/myFile.xml", "r");
while($fileContent = fread($fp, 1024*1024*5))
{
    if (!xml_parse($xmlParser, $fileContent, feof($fp)))
    {
        $xmlErreurString = xml_error_string(xml_get_error_code($xmlParser));
    }
}

这篇关于xml_parse没有内存错误PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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