7

I use Maatwebsite to load excel files. I'm loading fine several documents, until it gives the next error:

PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) in /project1/vendor/phpoffice/phpexcel/Classes/PHPExcel/Cell.php on line 582

I'm went to config/excel.php and update memoryCacheSize setting to 128MB. After do that I run php artisan cache:clear and php artisan config:cache. Try load again and returns me the same error.

How can I solve that?

user3242861
  • 1,839
  • 12
  • 48
  • 93
  • Your excel files are too big? BTW share the code too, not only the error. – Troyer Apr 05 '18 at 14:19
  • 1
    You will need to increase the `memory_limit` in `php.ini` to say `256M` – RiggsFolly Apr 05 '18 at 14:22
  • I broke the main file in several small files. And It has worked well before, so I think it's not about the code. I think I have to clean cache about maatwebsite or phpexcel library but how? @Troyer – user3242861 Apr 05 '18 at 14:23

5 Answers5

4

I would recommend using box/spout for larger files. I have used both and typically use Maatwebsite for smaller files because of the flexibility of file types.

https://github.com/box/spout

Adam Rodriguez
  • 1,850
  • 1
  • 12
  • 15
  • 1
    You can also use https://github.com/rap2hpoutre/fast-excel which is a Laravel wrapper over Spout. – rap-2-h Aug 30 '19 at 10:37
3

Add this in your index.php file.

ini_set('memory_limit', '-1');
Giri Annamalai M
  • 810
  • 9
  • 24
  • M It work good for a period of time. But now doesn't work... returns me this to a file with 10 lines and for a file with 1000 doesn't start loading file... – user3242861 Apr 09 '18 at 12:55
  • 1
    -1 meant as much as available memory will be allocated. There must some error will be returned. Post that here we can find it. I have met problem, as the temp directory not writable after some days. So i changed the temp to somewhere. Its worked. Just post the error you have met. – Giri Annamalai M Apr 09 '18 at 14:08
3

You have to increase the memory limit of PHP. You have many ways to do this.

First if you want to check the actual memory limit you need to create a PHP file (we can name it as php.php) or put it in an action inside any controller, and put following code in it:

<?php phpinfo(); exit;?>

First way to change memory_limit, this way modify the momeoty_limit for all proyects running in PHP:

Firstly you need find your php.ini, this data is in the phpinfo();

enter image description here

  1. Edit php.ini .Search "memory_limit" in your php.ini, and change the value of it. If no "memory_limit" found, add the following line at the end of php.ini memory_limit = 128M ; /* Change the 128M to your needs */ Save file.

  2. Reset apache.

    sudo service apache2 restart

Second option is to put it in the index.php inside your laravel proyect as @Giri Annamalai M tell you above.

ini_set('memory_limit', '-1');

This option will modify the memory limit to all proyect... If you put -1, this means that there is no limit.(dependes in compute memory)

Other option is to indicate the memory limit in the controller, inside the action indicete the memory limit:

ini_set('memory_limit', '-1');

Remember that -1 is always NO LIMIT. Maybe this is no the best secure and performance way to indicate the memory limit. You have other ways to edit the memory like the .htaccess... But i think that the 3rd is one of the best in this ocation.

TacticJuls
  • 304
  • 1
  • 8
1

In 2021 with version 3.1 non of this works.

I got it working with WithChunkReading.

PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
0

I did some debugging to see what scenarios decreased the memory usage of Laravel Excel.

My best results where using a DTO, one that only contained simple variable types. Casting objects like Carbon or BigDecimal to string also saved some memory usage.

in the Export you can remove the withMapping trait because the DTO properties will do that trick for you. If you want you can change the order of the DTO's properties.

And remove the ShouldAutoSize trait, as suggested by the maintainer

e.g.

class Row {
  public int $id;
  public string $created_at;
}
class UserExport implements FromCollection
{
    public function collection()
    {
        $users = User::query()->all();

        return $users->map(
            function ($user) {
                $row = new Row();
                $row->id = $user->id;
                // cast objects like Carbon or BigDecimal to string
                $row->created_at = $entry->created_at->format('d-m-Y');

                return $row;
            }
        );
    }
}
Tomas
  • 550
  • 6
  • 17