| PHP Developer Forum Hier habt ihr die Möglichkeit, eure Skriptprobleme mit anderen Anwendern zu diskutieren. Seid so fair und beantwortet auch Fragen von anderen Anwendern. Dieses Forum ist sowohl für ANFÄNGER als auch für PHP-Profis! Post your PHP questions here! |
 |

24-05-2005, 11:13
|
|
iamnils
Newbie
|
|
Registriert seit: Jan 2005
Beiträge: 9
|
|
Automatisierte Konvertierung (dbf->csv)
Hallo zusammen,
ich bin auf der Suche nach einer Möglichkeit, eine Datei zu konvertieren. Das Ausgangsformat ist dbf, darauf soll eine csv-Datei erstellt werden.
Vorher muss noch die Datei entzippt werden, dies werde ich zuerst mal mit der Funktion zip_open() ausprobieren. Gibt es auch noch andere Möglichkeiten?
mfg Nils
|

24-05-2005, 12:01
|
|
asp2php
Banned
|
|
Registriert seit: Feb 2004
Beiträge: 11.746
|
|
|

24-05-2005, 12:29
|
|
Fadil
Newbie
|
|
Registriert seit: Jan 2004
Ort: Austria
Beiträge: 15
|
|
Versuche das, ich habe es auch nicht getestet, keine ahnung ob es geht.
Ich hab noch nie was mit "dbf" gemacht!
PHP-Code:
$dbfh = dbase_open("file.dbf",0);
$fh = fopen("file.csv", "w");
$fields = array_keys(dbase_get_record_with_names($dbfh, 1));
fputcsv($fh, $fields);
for($i=1; $i<dbase_numrecords($dbfh); ++$i)
fputcsv($fh,dbase_get_record($dbfh, $i))
fclose($fh);
dbase_close($dbfh);
__________________
Arbeite mit, Plane mit, Regiere mit!
|

24-05-2005, 14:06
|
|
iamnils
Newbie
|
|
Registriert seit: Jan 2005
Beiträge: 9
|
|
Hallo,
ich habe mal die Codes probiert, aber das Script funktioniert nicht.
"Call to undefined function: dbase_open() "
Gäbe es eine andere Möglichkeit umzuwandeln?
Zu Gzip: ich muss einfach den normalen gzip-befehl zum entpacken per exec() ausführen oder muss ich etwas beachten?
mfg Nils
|

24-05-2005, 15:47
|
|
Fadil
Newbie
|
|
Registriert seit: Jan 2004
Ort: Austria
Beiträge: 15
|
|
Es funktioniert bei dir nicht weil die nötige bibiliothek nich installiert ist.
Andere möglichkeit wäre die unten, und diesmal habe ich es auch getestet!
Du solltest die klassendefinition auf jeden fall in irgend ein file auslagern. Der lösungscode befindet sich ganz unten.
PHP-Code:
<?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: [url]http://www.clicketyclick.dk/databases/xbase/format/dbf.html#DBF_STRUCT[/url]
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.
**************************************************************/
class dbf_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 dbf_class($filename) {
if ( !file_exists($filename)) {
echo 'Not a valid DBF file !!!'; exit;
}
$tail=substr($filename,-4);
if (strcasecmp($tail, '.dbf')!=0) {
echo 'Not 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 'Not 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++) {
$col=trim(substr($rawrow,$beg,$this->dbf_names[$i]['len']));
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++) {
$col=trim(substr($rawrow,$beg,$this->dbf_names[$i]['len']));
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
#include("dbf_class.php");
$file= "test.dbf";
$csv = fopen("file.csv","w");
$dbf = new dbf_class($file);
$num_rec=$dbf->dbf_num_rec;
fputcsv($csv, array_keys($dbf->getRowAssoc(0)));
for($i=0; $i<$num_rec; $i++)
fputcsv($csv, $dbf->getRow($i));
fclose($csv);?>
__________________
Arbeite mit, Plane mit, Regiere mit!
|

24-05-2005, 16:07
|
|
Fadil
Newbie
|
|
Registriert seit: Jan 2004
Ort: Austria
Beiträge: 15
|
|
Funktion "fputcsv()" geht erst mit php5.
Wenn du ältere version hast:
PHP-Code:
$file= "test.dbf";
$csv = fopen("file.csv","w");
$csv_trenner = ";";
$dbf = new dbf_class($file);
$num_rec=$dbf->dbf_num_rec;
fwrite($csv, implode($csv_trenner,array_keys($dbf->getRowAssoc(0)))."\n");
for($i=0; $i<$num_rec; $i++)
fwrite($csv, implode($csv_trenner,$dbf->getRow($i))."\n");
fclose($csv);
Entschuldigung wegen sprachfehler. Ich nix Deutscher, ich Ausländer und sprachlich total unbegabt  . Gilt für viele andere bereiche auch!
__________________
Arbeite mit, Plane mit, Regiere mit!
Geändert von Fadil (24-05-2005 um 16:21 Uhr)
|

25-05-2005, 14:48
|
|
iamnils
Newbie
|
|
Registriert seit: Jan 2005
Beiträge: 9
|
|
Hallo Fadil,
das Script klappt super! Vielen Dank.
Jetzt muss ich nur noch eine Zip-Class finden um die Datei vorher zu entpacken. Ich habe mir mal die Class von Vincent Blavet angeschaut, die braucht PEAR, welches bei mir auch installiert ist. Aber ich finde leider keine Doku.
Kennt ihr vielleicht noch eine gute Klasse, wofür ich keine zip-lib benötige?
mfg Nils
|

25-05-2005, 16:42
|
|
iamnils
Newbie
|
|
Registriert seit: Jan 2005
Beiträge: 9
|
|
Hallo,
isch hanns geschafft.
Fertiges Script:
PHP-Code:
// Herstellen der Basis-Verbindung
$conn_id1 = fopen("$url", "r");
$conn_id2 = fopen("$url_zip", "w");
// Lesen der Datei
while(!feof($conn_id1))
{
$download = fgets($conn_id1);
if(!fwrite($conn_id2, $download))
{
echo "Kann in die Datei $url_zip nicht schreiben<br>";
exit;
}
}
// Entpacken des Zip-Archives
shell_exec('unzip '.$url_zip.' -d '.$intern[ftp_server]);
// Umwandlung dbf->csv
$csv = fopen($url_csv,"w");
$csv_trenner = " ";
$dbf = new dbf_class($url_dbf);
$num_rec=$dbf->dbf_num_rec;
fwrite($csv, implode($csv_trenner,array_keys($dbf->getRowAssoc(0)))."\n");
for($i=0; $i<$num_rec; $i++)
fwrite($csv, implode($csv_trenner,$dbf->getRow($i))."\n");
fclose($csv);
// Eintrag der letzten Änderung in die Datenbank
mysql_query("UPDATE Artikel_Aktualisierung SET Datum = '$_SYS[UnixTime]' WHERE zu_Distri_ID = '3'", $_SYS[db]);
// Schließen des Streams
fclose($conn_id1);
fclose($conn_id2);
// Löschen von Dateien
unlink($url_zip);
unlink($url_dbf);
mfg Nils
|

25-05-2005, 16:54
|
|
iamnils
Newbie
|
|
Registriert seit: Jan 2005
Beiträge: 9
|
|
Habe doch noch ein Problem,
Sonderzeichen wie ä,ö,ü,ß werden nicht korrekt umgesetzt. Ich kann jetzt allerdings nicht beschreiben, wie die Buchstaben aussehen, welche Codes dahinterstehen nicht. Wenn ihr mir sagt, wie ich das rausbekomme, mache ich das sofort.
ä = anführungsstriche unten
ö = anführungsstriche oben
ü = rechteck
ß = á
mfg Nils
|

26-05-2005, 15:24
|
|
Fadil
Newbie
|
|
Registriert seit: Jan 2004
Ort: Austria
Beiträge: 15
|
|
Bei mir sehen ale sonderzeichen super aus.
Wie schaut die csv datei in einem einfachen Texteditor aus.
__________________
Arbeite mit, Plane mit, Regiere mit!
|
|
Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)
|
|
|
| Themen-Optionen |
|
|
| Thema bewerten |
|
|
Forumregeln
|
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.
HTML-Code ist aus.
|
|
|
|
PHP News
|