-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase_class.php
More file actions
180 lines (166 loc) · 7.44 KB
/
Base_class.php
File metadata and controls
180 lines (166 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/************************************************************
DBF reader Class v0.04 by Faro K Rasyid (Orca)
orca75_at_dotgeek_dot_org
v0.05 by Nicholas Vrtis
vrtis_at_vrtisworks_dot_com
1) changed to not read in complete file at creation.
2) added function to read individual rows
3) added support for Memo fields in dbt files.
4) See: http://www.clicketyclick.dk/databases/xbase/format/dbf.html#DBF_STRUCT
for some additional information on XBase structure...
5) NOTE: the whole file (and the memo file) is read in at once. So this could
take a lot of memory for large files.
Input : name of the DBF( dBase III plus) file
Output : - dbf_num_rec, the number of records
- dbf_num_field, the number of fields
- dbf_names, array of field information ('name', 'len', 'type')
Usage example:
$file= "your_file.dbf";//WARNING !!! CASE SENSITIVE APPLIED !!!!!
$dbf = new dbf_class($file);
$num_rec=$dbf->dbf_num_rec;
$num_field=$dbf->dbf_num_field;
for($i=0; $i<$num_rec; $i++){
$row = $dbf->getRow($i);
for($j=0; $j<$num_field; $j++){
echo $row[$j].' ');
}
echo('<br>');
}
Thanks to :
- Willy
- Miryadi
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
update by syt
**************************************************************/
class Base_class {
var $dbf_num_rec; //Number of records in the file
var $dbf_num_field; //Number of columns in each row
var $dbf_names = array(); //Information on each column ['name'],['len'],['type']
//These are private....
var $_raw; //The raw input file
var $_rowsize; //Length of each row
var $_hdrsize; //Length of the header information (offset to 1st record)
var $_memos; //The raw memo file (if there is one).
function Base_class($filename) {
if ( !file_exists($filename)) {
echo '1Not a valid DBF file !!!'; exit;
}
$tail=substr($filename,-4);
if (strcasecmp($tail, '.dbf')!=0) {
echo '2Not a valid DBF file !!!'; exit;
}
//Read the File
$handle = fopen($filename, "r");
if (!$handle) { echo "Cannot read DBF file"; exit; }
$filesize = filesize($filename);
$this->_raw = fread ($handle, $filesize);
fclose ($handle);
//Make sure that we indeed have a dbf file...
if(!(ord($this->_raw[0]) == 3 || ord($this->_raw[0]) == 131) && ord($this->_raw[$filesize]) != 26) {
// echo '3Not a valid DBF file !!!'; exit;
}
// 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];
}
if (ord($this->_raw[0])==131) { //See if this has a memo file with it...
//Read the File
$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;
$handle = fopen($memoname, "r");
if (!$handle) { echo "Cannot read DBT file"; exit; }
$filesize = filesize($memoname);
$this->_memos = fread ($handle, $filesize);
fclose ($handle);
}
}
function getRow($recnum) {
$memoeot = chr(26).chr(26);
$rawrow = substr($this->_raw,$recnum*$this->_rowsize+$this->_hdrsize,$this->_rowsize);
$rowrecs = array();
$beg=1;
if (ord($rawrow[0])==42) {
return false; //Record is deleted...
}
for ($i=0; $i<$this->dbf_num_field; $i++) {
switch($this->dbf_names[$i]['type']){
case 'C':$col=trim(substr($rawrow,$beg,$this->dbf_names[$i]['len']));break;
case 'T':$col=unpack('Vdate/Vsecond',(substr($rawrow,$beg,$this->dbf_names[$i]['len'])));
$timestamp = jdtounix($col['date']);
$col = date('Y-m-d H:i:s', $timestamp);break;
default:$col=trim(substr($rawrow,$beg,$this->dbf_names[$i]['len']));break;
}
if ($this->dbf_names[$i]['type']!='M') {
$rowrecs[]=$col;
} else {
$memobeg=$col*512; //Find start of the memo block (0=header so it works)
$memoend=strpos($this->_memos,$memoeot,$memobeg); //Find the end of the memo
$rowrecs[]=substr($this->_memos,$memobeg,$memoend-$memobeg);
}
$beg+=$this->dbf_names[$i]['len'];
}
return $rowrecs;
}
function getRowAssoc($recnum) {
$rawrow = substr($this->_raw,$recnum*$this->_rowsize+$this->_hdrsize,$this->_rowsize);
$rowrecs = array();
$beg=1;
if (ord($rawrow[0])==42) {
return false; //Record is deleted...
}
for ($i=0; $i<$this->dbf_num_field; $i++) {
switch($this->dbf_names[$i]['type']){
case 'C':$col=trim(substr($rawrow,$beg,$this->dbf_names[$i]['len']));break;
case 'T':$col=unpack('Vdate/Vsecond',(substr($rawrow,$beg,$this->dbf_names[$i]['len'])));
$timestamp = jdtounix($col['date']);
$col = date('Y-m-d H:i:s', $timestamp);break;
default:$col=trim(substr($rawrow,$beg,$this->dbf_names[$i]['len']));break;
}
if ($this->dbf_names[$i]['type']!='M') {
$rowrecs[$this->dbf_names[$i]['name']]=$col;
} else {
$memobeg=$col*512; //Find start of the memo block (0=header so it works)
$memoend=strpos($this->_memos,$memoeot,$memobeg); //Find the end of the memo
$rowrecs[$this->dbf_names[$i]['name']]=substr($this->_memos,$memobeg,$memoend-$memobeg);
}
$beg+=$this->dbf_names[$i]['len'];
}
return $rowrecs;
}
}//End of Class
?>