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?

webbear
  • 429
  • 1
  • 6
  • 12

3 Answers3

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));
    }
}
Laurent
  • 96
  • 1
  • 3
  • 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! – Mikko Rantalainen Oct 11 '21 at 09:10
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.

webbear
  • 429
  • 1
  • 6
  • 12
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.

ckm
  • 1,326
  • 10
  • 15
  • Not sure why this was downvoted as it is the correct answer for newer versions of PHP..... – ckm Nov 16 '20 at 02:59