Recommend this page to a friend! |
DBF Class | > | All threads | > | function dbf_class() | > | (Un) Subscribe thread alerts |
|
Uldis Nelsons - 2011-09-15 13:34:25
function dbf_class($filename) {
$this->sError = ''; if ( !file_exists($filename)) { $this->sError = 'Not exist DBF file: ' . $filename . '!!!'; return False; } $tail=substr($filename,-4); if (strcasecmp($tail, '.dbf')!=0) { $this->sError = 'Invalid extension for DBF file ' . $filename . '!!!'; return False; } //Read the File $handle = fopen($filename, "r"); if (!$handle) { $this->sError = 'Cannot read DBF file ' . $filename . '!!!'; return False; } $filesize = filesize($filename); echo '$filesize:'.$filesize.PHP_EOL; $this->_raw = fread ($handle, $filesize); fclose ($handle); if ($this->_raw === FALSE) { $this->sError = 'if ($this->_raw === FALSE) !!!'; return False; } //Make sure that we indeed have a dbf file... if(!isset($this->_raw[0]) ) { $this->sError = 'if(!isset($this->_raw[0]) ) !!!'; return False; } if( !isset($this->_raw[$filesize-1])) { $this->sError = 'if( !isset($this->_raw[$filesize])) !!!'; return False; } if(!( ord($this->_raw[0]) == 3 || //FoxBASE+/Dbase III plus, no memo ord($this->_raw[0]) == 131 || //FoxBASE+/dBASE III PLUS, with memo ord($this->_raw[0]) == 48) //Visual FoxPro ) { $this->sError = 'Not a valid DBF file !!!'; return False; } // 3= file without DBT memo file; 131 ($83)= file with a DBT. $arrHeaderHex = array(); for($i=0; $i<32; $i++){ $arrHeaderHex[$i] = str_pad(dechex(ord($this->_raw[$i]) ), 2, "0", STR_PAD_LEFT); } //Initial information $line = 32;//Header Size //Number of records $this->dbf_num_rec= hexdec($arrHeaderHex[7].$arrHeaderHex[6].$arrHeaderHex[5].$arrHeaderHex[4]); $this->_hdrsize= hexdec($arrHeaderHex[9].$arrHeaderHex[8]);//Header Size+Field Descriptor //Number of fields $this->_rowsize = hexdec($arrHeaderHex[11].$arrHeaderHex[10]); $this->dbf_num_field = floor(($this->_hdrsize - $line ) / $line ) ;//Number of Fields //Field properties retrieval looping for($j=0; $j<$this->dbf_num_field; $j++){ $name = ''; $beg = $j*$line+$line; for($k=$beg; $k<$beg+11; $k++){ if(ord($this->_raw[$k])!=0){ $name .= $this->_raw[$k]; } } $this->dbf_names[$j]['name']= $name;//Name of the Field $this->dbf_names[$j]['len']= ord($this->_raw[$beg+16]);//Length of the field $this->dbf_names[$j]['type']= $this->_raw[$beg+11]; } /** * momo file */ $memoname = ''; /** * FoxBASE+/dBASE III PLUS, with memo */ if (ord($this->_raw[0]) == 131) { //create filename $tail = substr($tail, -1, 1); //Get the last character... if ($tail == 'F') { //See if upper or lower case $tail = 'T'; //Keep the case the same } else { $tail = 't'; } $memoname = substr($filename, 0, strlen($filename) - 1) . $tail; } /** * Visual FoxPro with memo */ elseif (ord($this->_raw[28]) & 2) { $tail = substr($tail, -1, 1); if ($tail == 'F') { //See if upper or lower case $tail = 'FPT'; //Keep the case the same } else { $tail = 'fpt'; } $memoname = substr($filename, 0, strlen($filename) - 3) . $tail; } /** * read memo file im memmory */ if (!empty($memoname)) { $handle = fopen($memoname, "r"); if (!$handle) { echo "Cannot read DBT file"; exit; } $filesize = filesize($memoname); $this->_memos = fread($handle, $filesize); fclose($handle); } } |
info at phpclasses dot org
.