classCsvreader {Private $csv _file; Private $SPL _object=NULL; Private $error; Public function__construct ($csv _file= ' ') { if($csv _file&&file_exists($csv _file)) { $this->csv_file =$csv _file; } } Public functionSet_csv_file ($csv _file) { if(!$csv _file|| !file_exists($csv _file)) { $this->error = ' File invalid '; return false; } $this->csv_file =$csv _file; $this->spl_object =NULL; } Public functionGet_csv_file () {return $this-Csv_file; } Private function_file_valid ($file= ' ') { $file=$file?$file:$this-Csv_file; if(!$file|| !file_exists($file)) { return false; } if(!is_readable($file)) { return false; } return true; } Private function_open_file () {if(!$this-_file_valid ()) { $this->error = ' File invalid '; return false; } if($this->spl_object = =NULL) { $this->spl_object =NewSplfileobject ($this->csv_file, ' RB '); } return true; } Public functionGet_data ($length= 0,$start= 0) { if(!$this-_open_file ()) { return false; } $length=$length?$length:$this-Get_lines (); $start=$start-1; $start= ($start< 0)? 0:$start; $data=Array(); $this->spl_object->seek ($start); while($length--&&!$this->spl_object->EOF ()) { $data[] =$this->spl_object->Fgetcsv(); $this->spl_object->Next(); } return $data; } Public functionGet_lines () {if(!$this-_open_file ()) { return false; } $this->spl_object->seek (filesize($this-csv_file)); return $this->spl_object->Key(); } Public functionGet_error () {return $this-error; }}
CSV file Read class