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

   2023-02-09 学习力0
核心提示:原帖为英文,地址: http://***.com/questions/1947348/how-to-read-foxpro-memo-with-php测试可正常读取memory数据,特转帖供有需要的同学参考,并向原作者致谢!以下仅将内容粘贴过来,并未整理。   1down votefavorite3I have to convert .DBF and .FPT

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

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

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

 

 

 

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://***.com/questions/1947348/how-to-read-foxpro-memo-with-php

 

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • php-fpm进程管理的三种模式 phpfpm子进程
    php-fpm进程管理的三种模式 phpfpm子进程
    php-fpm解读-进程管理的三种模式—程序媛大丽标明转载以示尊重 感谢原作者的分享。php-fpm进程管理一共有三种模式:ondemand、static、dynamic,我们可以在同一个fpm的master配置三种模式,看下图1。php-fpm的工作模式和nginx类似,都是一个master,多个worke
    03-08
  • nginx和php-fpm 是使用 tcp socket 还是 unix s
    tcp socket允许通过网络进程之间的通信,也可以通过loopback进行本地进程之间通信。unix socket允许在本地运行的进程之间进行通信。分析从上面的图片可以看,unix socket减少了不必要的tcp开销,而tcp需要经过loopback,还要申请临时端口和tcp相关资源。但是
    03-08
  • [PHP8] 我参加了PHP8工程师认证初学者考试beta考试
    [PHP8] 我参加了PHP8工程师认证初学者考试beta
    前几天,2022/08/05,PHP工程师认证机构PHP8 技术员认证初级考试宣布实施考试将于 2023 年春季开始。和 beta 测试完成于 2022/09/11所以我收到了。一般社团法人BOSS-CON JAPAN(代表理事:Tadashi Yoshimasa,地点:东京都世田谷区,以下简称“BOSS-CON JAPAN
    03-08
  • 将 PHP Insights 放入旧版 PJ 不是很好吗?谈论
    将 PHP Insights 放入旧版 PJ 不是很好吗?谈论
    介绍在最近的PHP系统开发中,感觉故事在理所当然包含静态分析工具的前提下进行。我的周围现有代码很脏,我很久以前安装了工具,但几乎没有检查已经观察到许多这样的案例。 (这是小说。而不是像 0 或 100 这样不允许单行错误的静态分析,一点一点,逐渐我想介
    03-08
  • PHP基于elasticsearch全文搜索引擎的开发 php使
    1.概述:全文搜索属于最常见的需求,开源的 Elasticsearch (以下简称 Elastic)是目前全文搜索引擎的首选。Elastic 的底层是开源库 Lucene。但是,你没法直接用 Lucene,必须自己写代码去调用它的接口。Elastic 是 Lucene 的封装,提供了 REST API 的操作接
    02-09
  • php视图操作
    一、视图的基本介绍         视图是虚拟的表。与包含数据的表不一样,视图只包含使用时动态检索数据的查询。        使用视图需要MySQL5及以后的版本支持。        下面是视图的一些常见应用:        重用SQL语句;        简化复杂的S
    02-09
  • php中图像处理的常用函数 php图形图像处理技术
    php中图像处理的常用函数 php图形图像处理技术
    1.imagecreate()函数imagecreate()函数是基于一个调色板的画布。?php $im = imagecreate(200,80);                //创建一个宽200,高80的画布。$white = imagecolorallocate($im,225,35,180);     //设置画布的背景颜色imagegif($im);
    02-09
  • PHP安全之webshell和后门检测
    PHP安全之webshell和后门检测
    基于PHP的应用面临着各种各样的攻击:XSS:对PHP的Web应用而言,跨站脚本是一个易受攻击的点。攻击者可以利用它盗取用户信息。你可以配置Apache,或是写更安全的PHP代码(验证所有用户输入)来防范XSS攻击SQL注入:这是PHP应用中,数据库层的易受攻击点。防范
    02-09
  • php使用时间戳保存时间的意义 PHP获取时间戳
    时间戳记录的是格林尼治时间,使用date格式化的时候会根据你程序设置的不同时区显示不同的时间。如果使用具体时间,则还需要进行多一步转换。
    02-09
  • PHP 获取提交表单数据方法
    PHP $_GET 和 $_POST变量是用来获取表单中的信息的,比如用户输入的信息。PHP表单操作在我们处理HTML表单和PHP表单时,我们要记住的重要一点是:HTML页面中的任何一个表单元素都可以自动的用于PHP脚本:表单举例: htmlbodyform action="welcome.php" method
    02-09
点击排行