Laravel-excel: [BUG][QUESTION] On large xlsx import (over 10mb) simplexml_load_string(): internal error: Huge input lookup

Created on 7 Feb 2019  路  2Comments  路  Source: Maatwebsite/Laravel-Excel

Prerequisites

  • [ x] Able to reproduce the behaviour outside of your code, the problem is isolated to Laravel Excel.
  • [ x] Checked that your issue isn't already filed.
  • [ x] Checked if no PR was submitted that fixes this problem.

Versions

  • PHP version: 7.1.5
  • Laravel version: 5.7
  • Package version: 3.1

    Description

On large xlsx imports (over 10mb) I get an error of simplexml_load_string(): internal error: Huge input lookup
I set up an xlsx file import using ToCollectionon v3.1 and it works very well until I try to upload a file over 10MB (my file has 116,000 xlsx rows), at which point, I receive the following error:
simplexml_load_string(): Entity: line 2: parser error : internal error: Huge input lookup

I found a temp fix but I am searching for a permanent fix.
After doing some digging, I found that the error is generated because "Since version 2.7.3 libxml limits the maximum size of a single text node to 10MB".
https://stackoverflow.com/questions/5428055/xml-parse-huge-on-function-simplexml-load-string

-- but the limit can be overridden by setting the flag XML_PARSE_HUGE at the time when the default options for the libxml loader are set.

To find where the options can be set I dug down in to the package's own dependency of PhpOffice/PhpSpreadsheet

/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php:759

As a test, I can temporarily fix the problem in the vendor code and I get my import to work by adding the XML_PARSE_HUGE option 1) either right at line 759 or 2) its seems that a better location is to fix/add the option in the Settings file:

/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Settings:81

by adding this line:

$options = LIBXML_COMPACT | LIBXML_PARSEHUGE;
(see: https://github.com/PHPOffice/PhpSpreadsheet/pull/391)

above the existing code:

if ($options === null && defined('LIBXML_DTDLOAD')) {

        $options = LIBXML_DTDLOAD | LIBXML_DTDATTR;
    }
    self::$libXmlLoaderOptions = $options;

As a permanent solution I have tried to extend/override the settings file from my code but I have not been successful at finding a way to do it.

Any suggestions on how to override would be much appreciated. Also, I have outlined my research into the problem above so that it might help anybody else who has the same issue.

Finally, it would be great if this flag could be an option in the package's config.excel file.

Thanks in advance for any help you might provide on this issue.

Steps to Reproduce

Import an xlsx file larger than 10mb

Expected behavior:

Actual behavior:

Additional Information

Any additional information, configuration or data that might be necessary to reproduce the issue.

question

Most helpful comment

I got it to work by using the Auto register event listeners as described in the docs
https://laravel-excel.maatwebsite.nl/3.1/imports/extending.html

I added the following to my Import class:

use Maatwebsite\Excel\Concerns\RegistersEventListeners;

class XlsxImport implements ToCollection
{
use RegistersEventListeners;

public static function beforeImport(BeforeImport $event)
{
    $options = LIBXML_COMPACT | LIBXML_PARSEHUGE;

    \PhpOffice\PhpSpreadsheet\Settings::setLibXmlLoaderOptions($options);
}

//------

rest of code here

}

All 2 comments

I got it to work by using the Auto register event listeners as described in the docs
https://laravel-excel.maatwebsite.nl/3.1/imports/extending.html

I added the following to my Import class:

use Maatwebsite\Excel\Concerns\RegistersEventListeners;

class XlsxImport implements ToCollection
{
use RegistersEventListeners;

public static function beforeImport(BeforeImport $event)
{
    $options = LIBXML_COMPACT | LIBXML_PARSEHUGE;

    \PhpOffice\PhpSpreadsheet\Settings::setLibXmlLoaderOptions($options);
}

//------

rest of code here

}

Closing the issue since it seems to be resolved. Please let us know and reopen the issue if you need any further assistance.

Was this page helpful?
0 / 5 - 0 ratings