Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^174) CHAPTER 11 ■ SPL FILE AND DIRECTORY HANDLING
} else {
$this->fp = fopen($filename, $mode, $use_include_path);
}
if(!$this->fp) {
throw new Exception("Cannot read file");
}
//Get the column map
$this->map = $this->fgetcsv();
$this->currentLine = 0;
}
function fgetcsv($delimiter = ',', $enclosure = '"') {
return fgetcsv($this->fp, 0, $delimiter, $enclosure);
}
function key() {
return $this->currentLine;
}
function current() {
/*



  • The fgetcsv method increments the file pointer

  • so you must first record the file pointer,

  • get the data, and return the file pointer.

  • Only the next() method should increment the

  • pointer during operation.
    /
    $fpLoc = ftell($this->fp);
    $data = $this->fgetcsv();
    fseek($this->fp, $fpLoc);
    return array_combine($this->map, $data);
    }
    function valid() {
    //Check for end-of-file
    if(feof($this->fp)) {
    return false;
    }
    /

  • Again, need to prevent the file pointer

  • from being advanced. This check prevents

  • a blank line at the end of file returning

  • as a null value.
    */
    McArthur_819-9C11.fm Page 174 Thursday, February 28, 2008 7:49 AM

Free download pdf