问题
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?
回答1:
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));
}
}
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/19152613/xml-parse-no-memory-error-php