3

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.

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?

3 Answers 3

8

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

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

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

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));
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

If you have the input in a string and you need to split it into fragments to feed to xml_parse() note that substr() may return boolean false instead of empty string with PHP version lesser than 8.0!
0

Problem is solved by downgrading of libxml. Due to our framework - Symfony 1.4 we need to use PHP 5.2.17 and libxml was last version. After downgrade everything is ok.

Comments

0

There is a better answer to this, apparently, as outlined here XML_PARSE_HUGE on function simplexml_load_string()

You need to set the constant LIBXML_PARSEHUGE to bypass the restriction:

$xmlDoc->loadXML( $xml , LIBXML_PARSEHUGE );

Thanks to @Vaclav Kohout for this usage note.

1 Comment

Not sure why this was downvoted as it is the correct answer for newer versions of PHP.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.