我正在使用PHPExcel加载一个大小为10 KB(40行和18列)的非常小的excel文件,但它总是给我内存大小错误。我知道文件大小非常小,因此代码中有问题但无法弄清楚。
ini_set('memory_limit', '-1');
$this->load->library('PHPExcel');
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
$cacheSettings = array( ' memoryCacheSize ' => '8MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
$data = excelToArray($upload_filename);
我使用了一个函数来加载文件内容并将其转换为数组:
function excelToArray($filePath, $header=true){
//Create excel reader after determining the file type
$inputFileName = $filePath;
/** Identify the type of $inputFileName **/
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
/** Create a new Reader of the type that has been identified **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
/** Set read type to read cell data onl **/
$objReader->setReadDataOnly(true);
/** Load $inputFileName to a PHPExcel Object **/
$objPHPExcel = $objReader->load($inputFileName);
//Get worksheet and built array with first row as header
$objWorksheet = $objPHPExcel->getActiveSheet();
//excel with first row header, use header as key
if($header){
$highestRow = $objWorksheet->getHighestDataRow();
$highestColumn = $objWorksheet->getHighestDataColumn();
$headingsArray = $objWorksheet->rangeToArray('A1:'.$highestColumn.'1',null, true, true, true);
$headingsArray = $headingsArray[1];
$r = -1;
$namedDataArray = array();
for ($row = 3; $row <= $highestRow; ++$row) {
$dataRow = $objWorksheet->rangeToArray('A'.$row.':'.$highestColumn.$row,null, true, true, true);
if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) {
++$r;
foreach($headingsArray as $columnKey => $columnHeading) {
$namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey];
}
}
}
}
else{
//excel sheet with no header
$namedDataArray = $objWorksheet->toArray(null,true,true,true);
}
return $namedDataArray;
}
我试图将其缩小到脚本失败的后续行:
$objPHPExcel = $objReader->load($inputFileName);
另一个要点是,此代码在localhost上正常工作,但在godaddy服务器上出错。
答案 0 :(得分:0)
1)您可以编辑php.ini中的memory_limit行(如果您有权访问该文件)将内存增加到64M:
memory_limit = 64M;
2)如果您无法访问php.ini文件,请将此行添加到.htaccess文件的顶部:
php_value memory_limit 64M
如果这不起作用或引发&#34; 500内部服务器错误&#34;,请从.htaccess中删除该行。