PHP致命错误:允许的内存耗尽

时间:2013-10-06 15:16:35

标签: php memory phpexcel

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes)

对于工作,我必须将几个xls和xlsx文件转换并重新格式化为csv。由于我很懒,并且实际上没有Excel的副本,我使用PHPExcel写了几行,它循环遍历我的硬盘上的目录,转换每个文件并将其保存到另一个目录。

该脚本运行良好,节省了大量时间,但如果要转换的文件超过4个,则会出现内存错误。

有没有办法在不再需要内存后清除内存中的数据?正如你在下面看到的,我尝试了一堆unsets,但它似乎没有帮助。

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 as $r){$csv .= implode(",", $r).",\n";}return $csv;}

$filesDir = dirname(__FILE__)."\\files";
$iterator = new DirectoryIterator($filesDir);
foreach ($iterator as $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 as $row_arr){
            foreach($row_arr as $k=>$v){

                //add lastname to new array
                if(strpos($k, "LASTNAME") !== false){
                    $rearr[$row_int][0] = $v;
                }
                //add firstname to array
                if(strpos($k, "FIRSTNAME") !== false){
                    $rearr[$row_int][1] = $v;
                }
                // add mi to 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 than 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);

    }
}

0 个答案:

没有答案
相关问题