PHP Fatal error: Allowed memory exhausted -


fatal error: allowed memory size of 134217728 bytes exhausted (tried allocate 32 bytes)

for work have convert , reformat several xls , xlsx files csv. since i'm lazy , don't own copy of excel wrote few lines using phpexcel loops through directory on hard drive, converts each file , saves directory.

the script works great, saves me loads of time, if there more 4 or files converted memory error.

is there way clear data memory after it's no longer needed? can see below, tried doing bunch of unsets didn't seem help.

error_reporting(e_all); ini_set('display_errors', true); ini_set('display_startup_errors', true); set_time_limit(0); require_once 'phpexcel.php'; require_once 'helper.php';  function swapext($file, $ext){ return str_replace(substr($file,strpos($file,".")),".".$ext,$file);} function makecsv($table){$csv = ""; foreach($table $r){$csv .= implode(",", $r).",\n";}return $csv;}  $filesdir = dirname(__file__)."\\files"; $iterator = new directoryiterator($filesdir); foreach ($iterator $fileinfo) {     if (!$fileinfo->isdot()) {          $xlsfile = $filesdir."\\".$fileinfo->getfilename();          $csv_filename = dirname(__file__)."\converted\\".basename(swapext($xlsfile,"csv"));          $fd = fopen ($csv_filename, "w");         if (!file_exists($xlsfile)) {             exit($xlsfile.": file doesn't exist.\n");         }         $arrxls = exceltoarray($xlsfile, true);         $rearr = array();         $cell_int = 0;         $row_int = 0;         foreach($arrxls $row_arr){             foreach($row_arr $k=>$v){                  //add lastname new array                 if(strpos($k, "lastname") !== false){                     $rearr[$row_int][0] = $v;                 }                 //add firstname array                 if(strpos($k, "firstname") !== false){                     $rearr[$row_int][1] = $v;                 }                 // add mi array                 if(strpos($k, "middlename") !== false){                     if(strlen($v) > 1){                         $rearr[$row_int][2] = substr($v,0,1);                        }else{                         $rearr[$row_int][2] = $v;                     }                 }                 //add address line 1                 if(strpos($k, "addressline1") !== false){                     $rearr[$row_int][3] = $v;                 }                 //add address line 2                 if(strpos($k, "addressline2") !== false){                     $rearr[$row_int][4] = $v;                 }                 //add city                 if(strpos($k, "city") !== false){                     $rearr[$row_int][5] = $v;                 }                 //state                 if(strpos($k, "state") !== false){                     $rearr[$row_int][6] = $v;                 }                 //zip                 if(strpos($k, "zipcode") !== false){                     if(strlen($v) > 5){                         //if it's longer 5 chars, set first 5                         $rearr[$row_int][7] = substr($v,0,5);                         $rearr[$row_int][8] = substr($v,-4);                     }else{                         $rearr[$row_int][7] = $v;                     }                 }                  $cell_int++;             }             if(!isset($rearr[$row_int][0])){$rearr[$row_int][0] = "";}             if(!isset($rearr[$row_int][1])){$rearr[$row_int][1] = "";}             if(!isset($rearr[$row_int][2])){$rearr[$row_int][2] = "";}             if(!isset($rearr[$row_int][3])){$rearr[$row_int][3] = "";}             if(!isset($rearr[$row_int][4])){$rearr[$row_int][4] = "";}             if(!isset($rearr[$row_int][5])){$rearr[$row_int][5] = "";}             if(!isset($rearr[$row_int][6])){$rearr[$row_int][6] = "";}             if(!isset($rearr[$row_int][7])){$rearr[$row_int][7] = "";}             if(!isset($rearr[$row_int][8])){$rearr[$row_int][8] = "";}             if(!isset($rearr[$row_int][9])){$rearr[$row_int][9] = "";}             if(!isset($rearr[$row_int][10])){$rearr[$row_int][10] = "";}             if(!isset($rearr[$row_int][11])){$rearr[$row_int][11] = "";}             if(!isset($rearr[$row_int][12])){$rearr[$row_int][12] = "";}                 $row_int++;         }         $filecontent = makecsv($rearr);         if(fputs($fd, $filecontent)){echo $xlsfile." converted.<br />";}         fclose($fd);           unset($fd); unset($filecontent); unset($rearr); unset($arrxls);      } } 


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -