note 109606 deleted from function.fgetcsv by hholzgra
| From: | hholzgra@php.net | Date: | Thu, 02 Aug 2012 12:46:41 +0000 |
| Subject: | note 109606 deleted from function.fgetcsv by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189693@lists.php.net to get a copy of this message | ||
Note Submitter: daniel dot boorn at gmail dot com
----
<?php
# code to split large csv with low memory usage
//set_time_limit(0);
error_reporting(E_ERROR | E_WARNING | E_PARSE);
//CSV File Name
$fileName = "import.csv";
//Max rows in file
$max = 100000;
$count = $split = 0;
$columns = array();
$maxFiles = 50;//failsafe
if (($handle = fopen($fileName, "r")) !== FALSE) {
while (($data = fgetcsv($handle)) !== FALSE) {
if($split>=$maxFiles) die('max files reached');
$count++;
if($count==1){//save columns
$columns = $data;
continue;
}
//close file is max rows reached
if($count>=$max){
fclose($fp);
unset($fp);
$count=0;
$split++;
}
//open new file for writting and add columns
if(!isset($fp) && $fp !== FALSE){
$fp = fopen("{$split}_{$fileName}",'w');
fputcsv($fp,$columns);
}
//save data to open file
fputcsv($fp,$data);
}
fclose($handle);//close main file
fclose($fp);//close split file
}