CSV file Read class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.