Tuesday, March 1, 2011

Reading multiple files with PHPExcel

Hello there.

I just recently started using this library (the one from CodePlex), but I ran into some issues. My goal is to use it so I can process some data from multiple Excel files, and send such data to a database, per file. I'm doing something like:

foreach( $file_list as $file ) {

    $book = PHPExcel_IOFactory::load( $path . $file );

}

So, inside the foreach I'm (for now) just showing the data to the user, but after five files, I get a memory error:

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 50688 bytes) in /var/www/test/classes/PHPExcel/Shared/OLERead.php on line 76

Is there a way to __destruct the object after each file is loaded, so space is reserved (made free) for the next file, instead of accumulating it, or do you rather know of a reason and work-around for this?

Please let me know any suggestions you have.

Thanks in advance.

From stackoverflow
  • This has been an issue for awhile, and it doesn't look like there's a way around it -- that is, unless someone has come up with something clever since the release of 5.3......

    "...it seems that PHP 5.3 will fix this. However, I would like to see a confirmation of this somewhere." [Oct 21 2008]

    (source) (more stuff)

    KaOSoFt : I can try PHP 5.3. I'll check and report back. Thanks!
    Stephen J. Fuhry : Even if PHP 5.3 supports stuff to do it, it could be unimplemented.. also, when that post was made, PHP 5.3 was still ~8 months away.. but who knows. Maybe replying to that post will stir some more activity :)
    KaOSoFt : Definitely fixed on PHP 5.3, though I tried this version on Windows, and my current development environment is on Linux. I'll try updating my Linux PHP build later. It will take its time to complete (200+ files), but it works. Thanks for the reference: couldn't have done it without your help!
  • The latest SVN code for PHPExcel (just checked in today) introduces cell caching to reduce memory usage... it's such a new feature, I haven't even had the time to document it yet. While the default method is identical to the present method, with the worksheet <--> cell relationship containing a cyclic reference, I believe that using any of the memory-reducing cache mechanisms should eliminate this cyclic reference. If not, let me know and I should be able to break the reference when unsetting a workbook/worksheet using some of the caching logic that already disables this connection when serializing the cells for caching.

    KaOSoFt : Mark, I replaced the Classes folder I had with the latest generated Source on CodePlex, and now I get this error message: Fatal error: Call to undefined function imagecreatefromstring() in /var/www/competencias/Classes/PHPExcel/Reader/Excel5.php on line 774
    Mark Baker : imagecreatefromstring() is a GD2 function, although the call to this function in PHPExcel hasn't changed in the latest release: in fact, it goes back even before 1.6.7. You need to ensure that GD2 is enabled in your PHP build.
    KaOSoFt : Weird. I haven't touched my original development environment, which means that GD2 was enabled before. I'll check phpinfo() and report back.
    KaOSoFt : Mark, I had to hard reset my computer. Looks like restarting apache2 service on Ubuntu wasn't enough. My software processes the files now, although I seem to be having some logic issues. Oh, well, I most likely ask about it in another Question. Thank you!

0 comments:

Post a Comment