1

this is my code, when i upload a file with more than 500kb in 100MB php_max_memory am getting Allowed memory size exhausted

public function runImport($file){   
         $errors = array();
                $objPHPExcel = Config::getExcel($file);
                foreach($objPHPExcel->getSheet(0)
                                    ->getRowIterator(2, $objPHPExcel->
                                                        getSheet(0)->
                                                        getHighestDataRow()) as $row ){
                    $i = 0;
                    $_data = array();
                    foreach($row->getCellIterator() as $cell ){
                        $i++;
                        if ($i > count($columns)) break;
                        array_push($_data,(string)$cell->getValue());
                    }
                    foreach ($_data as $item){
                        if ($item != null && $item != "" && strlen($item) > 0){
                            $jdata = new Jobsdata();
                            $jdata->jobId = $this->jobId;
                            $jdata->data = $_data;
                            $jdata->save();
                            break;
                        }
                    }
                }
                $this->save();
                return $errors;
        }
Paul Stanley
  • 4,018
  • 6
  • 35
  • 56
aex
  • 85
  • 1
  • 9
  • the file may be 2mb, but the amount of memory it takes when its actually loaded is likely to be considerably more. – Spudley Oct 21 '16 at 21:06
  • with using batch i successfully escaped the memory limit error – aex Oct 23 '16 at 14:25

1 Answers1

1

You might need to profile your script because PHPexcel tends to be a bit liberal with memory use, but in the meantime, if you are stuck for time, you can try a garbage collect with gc_collect_cycles() in your loops, since PHP doesnt garbage collect until a function or script finishes. It will slow it down, but it may finish.

public function runImport($file){   
     $errors = array();
            $objPHPExcel = Config::getExcel($file);
            foreach($objPHPExcel->getSheet(0)->getRowIterator(2, $objPHPExcel->getSheet(0)->getHighestDataRow()) as $row ){
                $i = 0;
                $_data = array();
                foreach($row->getCellIterator() as $cell ){
                    $i++;
                    if ($i > count($columns)) break;
                    array_push($_data,(string)$cell->getValue());
                    gc_collect_cycles();
                }
                foreach ($_data as $item){
                    if ($item != null && $item != "" && strlen($item) > 0){
                        $jdata = new Jobsdata();
                        $jdata->jobId = $this->jobId;
                        $jdata->data = $_data;
                        $jdata->save();
                        break;
                    }
                    gc_collect_cycles();
                }
                gc_collect_cycles();
            }
            $this->save();
            return $errors;
    }
Paul Stanley
  • 4,018
  • 6
  • 35
  • 56