Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xml_parse No memory error PHP

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?

like image 625
webbear Avatar asked Sep 08 '26 10:09

webbear


1 Answers

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));
    }
}
like image 67
Laurent Avatar answered Sep 10 '26 23:09

Laurent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!