【转帖】通过PHP读取dbf数据(visual fox pro,VFP数据库),官方的dbase无法读取字段为类型memo的数据,国外网站的解决方案 How to read FoxPro Memo with PHP?

原帖为英文,地址: http://stackoverflow.com/questions/1947348/how-to-read-foxpro-memo-with-php

测试可正常读取memory数据,特转帖供有需要的同学参考,并向原作者致谢!

以下仅将内容粘贴过来,并未整理。

How to read FoxPro Memo with PHP?


1down votefavorite

3

I have to convert .DBF and .FPT files from Visual FoxPro to MySQL. Right now my script works for .DBF files, it opens and reads them with dbase_open() and dbase_get_record_with_names() and then executes the MySQL INSERT commands.

However, some fields of these .DBF files are of type MEMO and therefore stored in a separate files ending in .FPT. How do I read this file?

I have found the specifications of this filetype in MSDN, but I don‘t know how I can read this file byte-wise with PHP (also, I would really prefer a simplier solution).

Any ideas?

Alright, I have carefully studied the MSDN specifications of DBF and FPT file structures and the outcome is a beautiful PHP class which can open a DBF and (optional) an FPT memo file at the same time. This class will give you record after record and thereby fetch any memos from the memo file - if opened.

  1 class Prodigy_DBF {
  2     private $Filename, $DB_Type, $DB_Update, $DB_Records, $DB_FirstData, $DB_RecordLength, $DB_Flags, $DB_CodePageMark, $DB_Fields, $FileHandle, $FileOpened;
  3     private $Memo_Handle, $Memo_Opened, $Memo_BlockSize;
  4
  5     private function Initialize() {
  6
  7         if($this->FileOpened) {
  8             fclose($this->FileHandle);
  9         }
 10
 11         if($this->Memo_Opened) {
 12             fclose($this->Memo_Handle);
 13         }
 14
 15         $this->FileOpened = false;
 16         $this->FileHandle = NULL;
 17         $this->Filename = NULL;
 18         $this->DB_Type = NULL;
 19         $this->DB_Update = NULL;
 20         $this->DB_Records = NULL;
 21         $this->DB_FirstData = NULL;
 22         $this->DB_RecordLength = NULL;
 23         $this->DB_CodePageMark = NULL;
 24         $this->DB_Flags = NULL;
 25         $this->DB_Fields = array();
 26
 27         $this->Memo_Handle = NULL;
 28         $this->Memo_Opened = false;
 29         $this->Memo_BlockSize = NULL;
 30     }
 31
 32     public function __construct($Filename, $MemoFilename = NULL) {
 33         $this->Prodigy_DBF($Filename, $MemoFilename);
 34     }
 35
 36     public function Prodigy_DBF($Filename, $MemoFilename = NULL) {
 37         $this->Initialize();
 38         $this->OpenDatabase($Filename, $MemoFilename);
 39     }
 40
 41     public function OpenDatabase($Filename, $MemoFilename = NULL) {
 42         $Return = false;
 43         $this->Initialize();
 44
 45         $this->FileHandle = fopen($Filename, "r");
 46         if($this->FileHandle) {
 47             // DB Open, reading headers
 48             $this->DB_Type = dechex(ord(fread($this->FileHandle, 1)));
 49             $LUPD = fread($this->FileHandle, 3);
 50             $this->DB_Update = ord($LUPD[0])."/".ord($LUPD[1])."/".ord($LUPD[2]);
 51             $Rec = unpack("V", fread($this->FileHandle, 4));
 52             $this->DB_Records = $Rec[1];
 53             $Pos = fread($this->FileHandle, 2);
 54             $this->DB_FirstData = (ord($Pos[0]) + ord($Pos[1]) * 256);
 55             $Len = fread($this->FileHandle, 2);
 56             $this->DB_RecordLength = (ord($Len[0]) + ord($Len[1]) * 256);
 57             fseek($this->FileHandle, 28); // Ignoring "reserved" bytes, jumping to table flags
 58             $this->DB_Flags = dechex(ord(fread($this->FileHandle, 1)));
 59             $this->DB_CodePageMark = ord(fread($this->FileHandle, 1));
 60             fseek($this->FileHandle, 2, SEEK_CUR);    // Ignoring next 2 "reserved" bytes
 61
 62             // Now reading field captions and attributes
 63             while(!feof($this->FileHandle)) {
 64
 65                 // Checking for end of header
 66                 if(ord(fread($this->FileHandle, 1)) == 13) {
 67                     break;  // End of header!
 68                 } else {
 69                     // Go back
 70                     fseek($this->FileHandle, -1, SEEK_CUR);
 71                 }
 72
 73                 $Field["Name"] = trim(fread($this->FileHandle, 11));
 74                 $Field["Type"] = fread($this->FileHandle, 1);
 75                 fseek($this->FileHandle, 4, SEEK_CUR);  // Skipping attribute "displacement"
 76                 $Field["Size"] = ord(fread($this->FileHandle, 1));
 77                 fseek($this->FileHandle, 15, SEEK_CUR); // Skipping any remaining attributes
 78                 $this->DB_Fields[] = $Field;
 79             }
 80
 81             // Setting file pointer to the first record
 82             fseek($this->FileHandle, $this->DB_FirstData);
 83
 84             $this->FileOpened = true;
 85
 86             // Open memo file, if exists
 87             if(!empty($MemoFilename) and file_exists($MemoFilename) and preg_match("%^(.+).fpt$%i", $MemoFilename)) {
 88                 $this->Memo_Handle = fopen($MemoFilename, "r");
 89                 if($this->Memo_Handle) {
 90                     $this->Memo_Opened = true;
 91
 92                     // Getting block size
 93                     fseek($this->Memo_Handle, 6);
 94                     $Data = unpack("n", fread($this->Memo_Handle, 2));
 95                     $this->Memo_BlockSize = $Data[1];
 96                 }
 97             }
 98         }
 99
100         return $Return;
101     }
102
103     public function GetNextRecord($FieldCaptions = false) {
104         $Return = NULL;
105         $Record = array();
106
107         if(!$this->FileOpened) {
108             $Return = false;
109         } elseif(feof($this->FileHandle)) {
110             $Return = NULL;
111         } else {
112             // File open and not EOF
113             fseek($this->FileHandle, 1, SEEK_CUR);  // Ignoring DELETE flag
114             foreach($this->DB_Fields as $Field) {
115                 $RawData = fread($this->FileHandle, $Field["Size"]);
116                 // Checking for memo reference
117                 if($Field["Type"] == "M" and $Field["Size"] == 4 and !empty($RawData)) {
118                     // Binary Memo reference
119                     $Memo_BO = unpack("V", $RawData);
120                     if($this->Memo_Opened and $Memo_BO != 0) {
121                         fseek($this->Memo_Handle, $Memo_BO[1] * $this->Memo_BlockSize);
122                         $Type = unpack("N", fread($this->Memo_Handle, 4));
123                         if($Type[1] == "1") {
124                             $Len = unpack("N", fread($this->Memo_Handle, 4));
125                             $Value = trim(fread($this->Memo_Handle, $Len[1]));
126                         } else {
127                             // Pictures will not be shown
128                             $Value = "{BINARY_PICTURE}";
129                         }
130                     } else {
131                         $Value = "{NO_MEMO_FILE_OPEN}";
132                     }
133                 } else {
134                     $Value = trim($RawData);
135                 }
136
137                 if($FieldCaptions) {
138                     $Record[$Field["Name"]] = $Value;
139                 } else {
140                     $Record[] = $Value;
141                 }
142             }
143
144             $Return = $Record;
145         }
146
147         return $Return;
148     }
149
150     function __destruct() {
151         // Cleanly close any open files before destruction
152         $this->Initialize();
153     }
154 }

The class can be used like this:

1 $Test = new Prodigy_DBF("customer.DBF", "customer.FPT");
2 while(($Record = $Test->GetNextRecord(true)) and !empty($Record)) {
3     print_r($Record);
4 }

It might not be an almighty perfect class, but it works for me. Feel free to use this code, but note that the class is VERY tolerant - it doesn‘t care if fread() and fseek() return true or anything else - so you might want to improve it a bit before using.

Also note that there are many private variables like number of records, recordsize etc. which are not used at the moment.

更多内容请查看原帖:http://stackoverflow.com/questions/1947348/how-to-read-foxpro-memo-with-php

时间: 2024-10-26 01:05:50

【转帖】通过PHP读取dbf数据(visual fox pro,VFP数据库),官方的dbase无法读取字段为类型memo的数据,国外网站的解决方案 How to read FoxPro Memo with PHP?的相关文章

[获取行数]php读取大文件提供性能的方法,PHP的stream_get_line函数读取大文件获取文件的行数的方...

背景: 下面是获取文件的行数的方法: 一个文件如果知道有几行的话,就可以控制获取一定的行数的数据,然后放入数据库.这样不管的读取大文件的性能,还是写入数据库的性能,都能得到很大的提高了. 下面是获取文件的行数的方法 $temp_file = 'error.log'; $fp = fopen($temp_file ,'r') or die("open file failure!"); $total_line = 0; if($fp){     /* 获取文件的一行内容,注意:需要php5

二进制数据将图片保存到数据库,并读取数据库二进制数据显示图片

一. 浏览图片 OpenFileDialog ofd = new OpenFileDialog();            ofd.InitialDirectory = @"E:\";            ofd.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files(*.*)|*.*";            ofd.RestoreDirectory = true; if (ofd

IntelliJ IDEA 在网页修改数据,但是在浏览器刷新的时候,不能读取到修改之后的数据

使用IntelliJ IDEA 在网页修改数据,但是在浏览器刷新的时候,不能读取到修改之后的数据? 解决办法:tomcat配置中,On frame deactivation属性选择Update classes and resources即可.

【转】Visual Studio 2010在数据库生成随机测数据

测试在项目中是很重要的一个环节,在Visual Studio 2010中,在测试方面已经有很好的支持了,比如有单元测试,负载测试等等.在数据测试的方面,Visual Studio 2010,还支持对数据库进行多种测试,其中一个很好用的功能是能为开发者在测试阶段,大量方便地为数据库生成随机的数据,而且还可以自己指定生成数据的规则,十分方便,这就让在测试过程中,开发者能有更充足的数据样本对项目进行测试.本文则介绍其中的Data Generation数据生成器的使用方法. 1 创建SQL Server

读取DBF文件的部分代码

private void BtnOpenInitial_Click(object sender, EventArgs e) { OpenFileDialog file = new OpenFileDialog(); if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string path = file.FileName; DTable dbf = new DTable(); dbf.Load(path); if (d

MySQL数据迁移到MSSQL-以小米数据库为例-测试828W最快可达到2分11秒

这里采用.NET Framework 4.0以上版本中新出现的 ConcurrentQueue<T> 类 MSDN是这样描述的: ConcurrentQueue<T> 类是一个线程安全的先进先出 (FIFO) 集合. ConcurrentQueue<T> 的所有公共且受保护的成员都是线程安全的,可从多个线程同时使用. 共采用两个线程,一个读一个写. ConcurrentQueue<T>的实现方法: (FIFO) 集合: ConcurrentQueue<

Visual Studio 单元测试之五---数据库测试

原文:Visual Studio 单元测试之五---数据库测试 数据库的单元测试主要是测试数据库中的数据是否符合特定的条件,Visual Studio 2010支持下面几种数据的单元测试类型(Visual Studio 2008 不支持数据库测试): 类型 说明 Data Checksum 对数据进行Checksum检验 Empty ResultSet 测试执行的SQL语句返回结果集是否为空 Execution Time 测试执行时间 Expected Schema 测试结果集中的列和数据类型是

【转载】C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte

C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte 转载:http://www.itdos.com/Mvc/20150302/0741255.html using System.IO; /// <summary> /// WebApi返回图片 /// </summary> public HttpResponseMessage GetQrCode() { var imgPath = @"D:\ITdosCom\Images

(转)大数据量高并发的数据库优化与sql优化

大数据量高并发的数据库优化 一.数据库结构的设计 如果不能设计一个合理的数据库模型,不仅会增加客户端和服务器段程序的编程和维护的难度,而且将会影响系统实际运行的性能.所以,在一个系统开始实施之前,完备的数据库模型的设计是必须的. 在一个系统分析.设计阶段,因为数据量较小,负荷较低.我们往往只注意到功能的实现,而很难注意到性能的薄弱之处,等到系统投入实际运行一段时间后,才发现系统的性能在降低,这时再来考虑提高系统性能则要花费更多的人力物力,而整个系统也不可避免的形成了一个打补丁工程. 所以在考虑整