Skip to content

Instantly share code, notes, and snippets.

@Mattamorphic
Created October 20, 2016 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mattamorphic/04d36a2de6ff9d099b1732df3b1521a5 to your computer and use it in GitHub Desktop.
Save Mattamorphic/04d36a2de6ff9d099b1732df3b1521a5 to your computer and use it in GitHub Desktop.
Optimised CSV file reading with PJP
<?php
Class CSVHandle
{
const CHUNK_SIZE = 4096;
private $chunk = [];
private $buffer = '';
private $fp = null;
/**
* Opens a file pointer and attaches this to the object
* @param string $filename
* @param string $modifier
**/
public function open(string $filename, string $modifier)
{
$this->fp = fopen($filename, $modifier);
}
/**
* Closes a file pointer attached to an object
**/
public function close()
{
if ($this->fp !== null) {
fclose($this->fp);
}
}
/**
* Returns the next line from a CSV file, either from the
* latest chunk in memory, or by setting up the next chunk
*
* @return array
**/
public function getLine() : array
{
if (empty($this->chunk)) {
if (feof($this->fp) {
return false;
}
// if we're empty get the next chunk using the previous buffer
$this->chunk = $this->buffer . fread(
$this->fp,
self::CHUNK_SIZE
);
// explode the chunk into lines
$this->chunk = preg_split("/\\r\\n|\\r|\\n/", $this->chunk);
// if this isn't the end of the file
if (!feof($this->fp)) {
// buffer out the last line
$this->buffer = array_pop($this->chunk);
}
}
// return the next line off the beginning of the stack
return (
"" === ($line = array_shift($this->chunk))
) ? false : str_getcsv($line);
}
/**
* Writes a line to a file pointer
**/
public function writeLine($values)
{
return fputcsv($this->fp, $values, ',', '\"');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment