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);
?>
nachdem auslesen der Header zeilen habe ich mir den rest in einem weiteren feld des Arrays erstellt.
$rest=$buff32.fread($fdbf, filesize($dbfname));
$dbfhdrarr[]=$rest;
nun kann man mit etwas String bearbeitung und einer Foreach schleife jedes Feld ändern.
Filename ist der Name der Originalen dbf-Datei.
Hier der komplette Code:
function get_dbf_header($dbfname) {
$fdbf = fopen($dbfname,'r');
$dbfhdrarr = array();
$buff32 = array();
$i = 1;
$j = 2;
if ($fdbf) $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('feldname'=>$fieldname,'feldtyp'=>$fieldtype,'feldlang'=>$fieldlen,'felddec'=>$fielddec));
}
}
else
{
$anfang=$buff32;
}
$i++;
} else {
$goon = false;
}
}
$rest=$buff32.fread($fdbf, filesize($dbfname));
$dbfhdrarr[]=$rest;
fclose($fdbf);
return($dbfhdrarr);
}
function GetRealSize($file) {
// Return size in Mb
clearstatcache();
$INT = 4294967295;//2147483647+2147483647+1;
$size = filesize($file);
$fp = fopen($file, 'r');
fseek($fp, 0, SEEK_END);
if (ftell($fp)==0) $size += $INT;
fclose($fp);
if ($size<0) $size += $INT;
return $size;
}
//-------------------------------------------------
//Werte Auslesen und Anpassen
//-------------------------------------------------
$arr = get_dbf_header($filevalue['filename']); //Kopfzeilen der DBF Auslesen
$rest=$arr[$filevalue['werteindex']]; //Werte der Zellen stehen in array Index 164
if (substr($rest,0,2)!=(chr(13).chr(32)))
{
$rest=chr(13).chr(32).substr($rest,265);
}
$speichern=chr(13).chr(32);//Zeilenumbruch und Leerzeichen vor erstem Datensatz
foreach ($arr as $key=>$value)
{
if (0==$key)
{
$wert=trim(substr($rest,2,($value['feldlang'])-2)); //Wert holen
$rest=substr($rest,($value['feldlang']+2)); //Restlichen String kürzen
}
else
{
$wert=trim(substr($rest,0,$value['feldlang'])); //Wert holen
$rest=substr($rest,$value['feldlang']); //Restlichen String kürzen
}
switch ($key)
{
case 0: $wert=''; //
break;
case 1: $wert=date('dmY'); //Datum
break;
case 2:
break;
case 3:
case 4:
}
}
Keine Kommentare:
Kommentar veröffentlichen