我正在創建一個將csv文件導入數據庫的函數。但是發生了一個錯誤,我想知道如何解決這個問題。php - 致命錯誤:允許的內存大小爲134217728字節耗盡
Error: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes)
我的功能:
importFile("ingram", "ingram_fees.txt", "ingram_fees");
function importFile($company, $fileName, $tableName) {
$filePath = "./ftp_imports/".$company."/".$fileName;
$file = fopen($filePath, "r");
$firstRowNames = fgetcsv($file);
$columnNames = array();
$rows = count($firstRowNames);
$i = 0;
while ($i < $rows) {
array_push($columnNames, toCamelCase($firstRowNames[$i]));
}
if ($result = $mysqli->query("SHOW TABLES LIKE '".$tableName."'")) {
if($result->num_rows !== 1) {
$queryCreateTable = "CREAT TABLE $tableName (";
$num = count($columnNames);
$i = 0;
while ($i < $num) {
switch (gettype($columnNames[$i])) {
case 'string':
$queryCreateTable .= $columnNames[$i] . " VARCHAR(25)";
break;
case 'integer':
$queryCreateTable .= $columnNames[$i] . " DECIMAL(10,2)";
break;
case 'double':
$queryCreateTable .= $columnNames[$i] . " DECIMAL(10,2)";
break;
case 'NULL':
$queryCreateTable .= $columnNames[$i] . " VARCHAR(25)";
break;
default:
break;
}
if ($i !== ($num - 1)) {
$queryCreateTable .= ",";
} else {
$queryCreateTable .= ");";
}
}
}
} else {//table already exists
$queryDelAll = "TRUNCATE TABLE $tableName";
$db->query($queryDelAll);
$queryImport = <<<eof
LOAD DATA LOCAL INFILE '$filePath'
INTO TABLE $tableName
FIELDS TERMINTED BY ','
LINES TERMINATED BY '\n'
INGORE 1 LINES
($columnNames)
eof;
$db->query($queryImport);
}
}
這給了,因爲它的內存使用量的誤差的功能。
function toCamelCase($string, $capitalizeFirstCharacter = false) {
$str = str_replace(' ', '', ucwords(str_replace('-', ' ', $string)));
if (!$capitalizeFirstCharacter) {
$str[0] = strtolower($str[0]);
}
return $str;
}
因爲$columnNames
陣列只有8個值長,我不知道爲什麼內存使用率是很高。
有人可以幫我嗎?
感謝, 每
..:
更好「);(例如'256M' for 256 Mb) –
您需要更新php ini memory_limit – fortune
'$ i'永遠不會在循環中增加,因此'$ columnNames'無限增長。 – Blackhole