Login   Register  
PHP Classes
elePHPant
Icontem

Removed requiremenet for dbase

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us

      Shape File Reader  >  All threads  >  Removed requiremenet for dbase  >  (Un) Subscribe thread alerts  
Subject:Removed requiremenet for dbase
Summary:Use this code if you need to remove dependence on dbase extensio
Messages:1
Author:Temistokles
Date:2012-12-31 02:23:07
 

  1. Removed requiremenet for dbase   Reply  
Picture of Temistokles
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;
}