©
This document uses PHP Chinese website manual Release
Many examples in this reference require a dBase database. We will use /tmp/test.dbf that will be created in the example of dbase_create() .
[#1] bi.idan [at] gmail.com [2009-02-12 07:16:31]
some of you contacted me about memo fields and my script, so i'll just post my answers on the common question here.
(note: this is not an adv for any program, only what i used for myself, and it's free)
so, there are 2 options if you want to create/read memo fields:
1. use a library for java/c++/c# to build a bridge between php and dbf file. i've used dbf2java. althougth it's not near complete as my other option, it's a good start. (http://code.google.com/p/dbf2java-library/)
2. use an external program with pipes to grab the output. i used cdbflite, (http://www.whitetown.com/cdbflite/). it's free, and pretty much gives you everything you need to handle those dbf files. i'm not sure about big databases and it seems you need to register/buy it, but it's again, a start.
hope it helps some of you who needs to handle those database files,
Good luck,
idan
[#2] bi.idan AT gmail.com [2007-04-16 16:40:35]
I know lots of you dosent really use dbase, but i've builded a class to help the one how dose.
(sorry for bad english)
- dbase.php
<?php
set_time_limit (0);
// site_path defined by parent
require_once (SITE_PATH. '/server/php/libs/dbase/handler.php');
class DBase
{
private $handler = false;
private $searchopt = array (); // Search optimizer
private function unload ()
{
if ($this-> handler !== false)
unset ($this-> handler);
}
public function __construct ($file = false)
{
if ($file !== false)
$this-> load ($file);
}
public function __destruct ()
{
$this-> unload ();
}
public function load ($file)
{
$resource = dbase_open ($file, 0);
$this-> handler = new DBase_Handler ($resource);
return $this-> handler;
}
public function search ($headerText, $string, $limit_results = false, $handler = false)
{
if ($handler === false)
$handler = $this-> handler;
if ($this-> searchopt [$headerText][$string])
return $this-> searchopt [$headerText][$string];
else
{
$size = $handler-> getSize ();
if ( ( $headerNumber = $handler-> getHeaderNumber ($headerText) ) !== false)
{
$results = array ();
for ($i = 1; $i < $size; $i++)
{
$record = $handler-> getRecord ($i, false); // Disabled optimizer to prevent memory overflow
if (trim ($record [$headerNumber]) == $string)
{
$results[] = $i;
if ( ($limit_results !== false) && (sizeof ($results) == $limit_results) )
break;
}
}
if (sizeof ($results) > 0)
{
$this-> searchopt [$headerText][$string] = $results;
return $this-> search ($headerText, $string, $handler);
}
return false;
} else
return false;
}
}
}
?>
- dbase_handler.php
<?php
class DBase_Handler
{
private $resource;
private $size; // Records Count
private $header = array ();
private $dataopt = array (); // Data optimizer
private function setHeader ()
{
$this-> header = dbase_get_header_info ($this-> resource);
}
public function __construct ($resource)
{
$this-> resource = $resource;
$this-> setHeader ();
$this-> size = dbase_numrecords ($this-> resource);
}
public function __destruct ()
{
dbase_close ($this-> resource);
}
public function getRecord ($record_number, $dataopt = true)
{
if ($record_number > $this-> size)
return false;
else
{
if ($this-> dataopt [$record_number])
return $this-> dataopt [$record_number];
else
{
$record = dbase_get_record ($this-> resource, $record_number);
if ($dataopt === true) // Data saving optimizer
{
$this-> dataopt [$record_number] = $record;
return $this-> getRecord ($record_number);
} else
return $record;
}
}
}
public function getHeaderNumber ($headerText)
{
foreach ($this-> header as $index => $header)
{
if ($header ['name'] == $headerText)
{
return $index;
break;
}
}
return false;
}
public function getHeader ($headerNumber)
{
if ($headerNumber <= sizeof ($this-> header))
return $this-> header [$headerNumber];
else
return false;
}
public function getSize ()
{
return $this-> size;
}
}
?>
[#3] Hadi Rusiah / deegos at yahoo dot com [2004-05-08 10:33:27]
If you are using PHP < 5, you can use this function to retrieve dbf header
<?php
function get_dbf_header($dbfname) {
$fdbf = fopen($dbfname,'r');
$dbfhdrarr = array();
$buff32 = array();
$i = 1;
$goon = true;
while ($goon) {
if (!feof($fdbf)) {
$buff32 = fread($fdbf,32);
if ($i > 1) {
if (substr($buff32,0,1) == chr(13)) {
$goon = false;
} else {
$pos = strpos(substr($buff32,0,10),chr(0));
$pos = ($pos == 0?10:$pos);
$fieldname = substr($buff32,0,$pos);
$fieldtype = substr($buff32,11,1);
$fieldlen = ord(substr($buff32,16,1));
$fielddec = ord(substr($buff32,17,1));
array_push($dbfhdrarr, array($fieldname,$fieldtype,$fieldlen,$fielddec));
}
}
$i++;
} else {
$goon = false;
}
}
fclose($fdbf);
return($dbfhdrarr);
}
$arr = get_dbf_header('/data/file.dbf');
print_r($arr);
?>