
 Temistokles - 2012-12-31 02:23:07
	public function getDbfData(){
                if (extension_loaded('dbase')) {
                    $this->_fetchDBFInformation();
                }
                else {
                    $this->_fetchDBFInformationNoDbase($this->file_name);
                }
                
                return $this->dbf_data;
	}
        //inspired by: http://php.net/manual/en/book.dbase.php
        private function _fetchDBFInformationNoDbase($dbfname) {
            $fdbf = fopen($dbfname, 'r');
            $fields = array();
            $buf = fread($fdbf, 32);
            $header = unpack("VRecordCount/vFirstRecord/vRecordLength", substr($buf, 4, 8));
            
            $goon = true;
            $unpackString = '';
            while ($goon && !feof($fdbf)) { // read fields: 
                $buf = fread($fdbf, 32);
                if (substr($buf, 0, 1) == chr(13)) {
                    $goon = false;
                } // end of field list 
                else {
                    $field = unpack("a11fieldname/A1fieldtype/Voffset/Cfieldlen/Cfielddec", substr($buf, 0, 18));
                    $unpackString.="A$field[fieldlen]$field[fieldname]/";
                    array_push($fields, $field);
                }
            }
            fseek($fdbf, $header['FirstRecord'] + 1); // move back to the start of the first record (after the field definitions) 
            
            fseek($fdbf, $header['RecordLength'] * ($this->record_number-1), SEEK_CUR);
            $buf = fread($fdbf, $header['RecordLength']);
            $data = unpack($unpackString, $buf);
            $this->dbf_data = $data;
        }