function create($filename, $fields) { if (!$fields || !is_array($fields)) { trigger_error("cannot create xbase with no fields", E_USER_ERROR); } $recordByteLength = 1; $columns = array(); $columnNames = array(); $i = 0; foreach ($fields as $field) { if (!$field || !is_array($field) || sizeof($field) < 2) { trigger_error("fields argument error, must be array of arrays", E_USER_ERROR); } $column = new XBaseColumn($field[0], $field[1], 0, @$field[2], @$field[3], 0, 0, 0, 0, 0, 0, $i, $recordByteLength); $recordByteLength += $column->getDataLength(); $columnNames[$i] = $field[0]; $columns[$i] = $column; $i++; } $result = new XBaseWritableTable($filename); $result->version = 131; $result->modifyDate = time(); $result->recordCount = 0; $result->recordByteLength = $recordByteLength; $result->inTransaction = 0; $result->encrypted = false; $result->mdxFlag = chr(0); $result->languageCode = chr(0); $result->columns = $columns; $result->columnNames = $columnNames; $result->backlist = ""; $result->foxpro = false; if ($result->openWrite($filename, true)) { return $result; } return false; }
function readHeader() { $this->version = $this->readChar(); $this->foxpro = $this->version == 48 || $this->version == 49 || $this->version == 245 || $this->version == 251; $this->modifyDate = $this->read3ByteDate(); $this->recordCount = $this->readInt(); $this->headerLength = $this->readShort(); $this->recordByteLength = $this->readShort(); $this->readBytes(2); //reserved $this->inTransaction = $this->readByte() != 0; $this->encrypted = $this->readByte() != 0; $this->readBytes(4); //Free record thread $this->readBytes(8); //Reserved for multi-user dBASE $this->mdxFlag = $this->readByte(); $this->languageCode = $this->readByte(); $this->readBytes(2); //reserved $fieldCount = ($this->headerLength - ($this->foxpro ? 296 : 33)) / 32; /* some checking */ if (!$this->isStream && $this->headerLength > filesize($this->name)) { trigger_error($this->name . " is not DBF", E_USER_ERROR); } if (!$this->isStream && $this->headerLength + $this->recordCount * $this->recordByteLength - 500 > filesize($this->name)) { trigger_error($this->name . " is not DBF", E_USER_ERROR); } /* columns */ $this->columnNames = array(); $this->columns = array(); $bytepos = 1; for ($i = 0; $i < $fieldCount; $i++) { $column = new XBaseColumn($this->readString(11), $this->readByte(), $this->readInt(), $this->readChar(), $this->readChar(), $this->readBytes(2), $this->readChar(), $this->readBytes(2), $this->readByte() != 0, $this->readBytes(7), $this->readByte() != 0, $i, $bytepos); $bytepos += $column->getLength(); $this->columnNames[$i] = $column->getName(); $this->columns[$i] = $column; } /**/ if ($this->foxpro) { $this->backlist = $this->readBytes(263); } $b = $this->readByte(); $this->recordPos = -1; $this->record = false; $this->deleteCount = 0; }