gpt4 book ai didi

php - 如何用 PHP 阅读 FoxPro 备忘录?

转载 作者:行者123 更新时间:2023-11-29 03:49:27 26 4
gpt4 key购买 nike

我必须将 .DBF 和 .FPT 文件从 Visual FoxPro 转换到 MySQL。现在我的脚本适用于 .DBF 文件,它使用 dbase_open() 和 dbase_get_record_with_names() 打开并读取它们,然后执行 MySQL INSERT 命令。

但是,这些 .DBF 文件的某些字段属于 MEMO 类型,因此存储在以 .FPT 结尾的单独文件中。我如何阅读这个文件?

我在 MSDN 中找到了此文件类型的规范,但我不知道如何使用 PHP 按字节读取此文件(另外,我真的更喜欢更简单的解决方案)。

有什么想法吗?

最佳答案

好的,我仔细研究了 DBF 和 FPT 文件结构的 MSDN 规范,结果是一个漂亮的 PHP 类,它可以同时打开 DBF 和(可选)FPT 备忘录文件。此类将为您提供一个接一个的记录,从而从备忘录文件中获取任何备忘录 - 如果打开的话。

class Prodigy_DBF {
private $Filename, $DB_Type, $DB_Update, $DB_Records, $DB_FirstData, $DB_RecordLength, $DB_Flags, $DB_CodePageMark, $DB_Fields, $FileHandle, $FileOpened;
private $Memo_Handle, $Memo_Opened, $Memo_BlockSize;

private function Initialize() {

if($this->FileOpened) {
fclose($this->FileHandle);
}

if($this->Memo_Opened) {
fclose($this->Memo_Handle);
}

$this->FileOpened = false;
$this->FileHandle = NULL;
$this->Filename = NULL;
$this->DB_Type = NULL;
$this->DB_Update = NULL;
$this->DB_Records = NULL;
$this->DB_FirstData = NULL;
$this->DB_RecordLength = NULL;
$this->DB_CodePageMark = NULL;
$this->DB_Flags = NULL;
$this->DB_Fields = array();

$this->Memo_Handle = NULL;
$this->Memo_Opened = false;
$this->Memo_BlockSize = NULL;
}

public function __construct($Filename, $MemoFilename = NULL) {
$this->Prodigy_DBF($Filename, $MemoFilename);
}

public function Prodigy_DBF($Filename, $MemoFilename = NULL) {
$this->Initialize();
$this->OpenDatabase($Filename, $MemoFilename);
}

public function OpenDatabase($Filename, $MemoFilename = NULL) {
$Return = false;
$this->Initialize();

$this->FileHandle = fopen($Filename, "r");
if($this->FileHandle) {
// DB Open, reading headers
$this->DB_Type = dechex(ord(fread($this->FileHandle, 1)));
$LUPD = fread($this->FileHandle, 3);
$this->DB_Update = ord($LUPD[0])."/".ord($LUPD[1])."/".ord($LUPD[2]);
$Rec = unpack("V", fread($this->FileHandle, 4));
$this->DB_Records = $Rec[1];
$Pos = fread($this->FileHandle, 2);
$this->DB_FirstData = (ord($Pos[0]) + ord($Pos[1]) * 256);
$Len = fread($this->FileHandle, 2);
$this->DB_RecordLength = (ord($Len[0]) + ord($Len[1]) * 256);
fseek($this->FileHandle, 28); // Ignoring "reserved" bytes, jumping to table flags
$this->DB_Flags = dechex(ord(fread($this->FileHandle, 1)));
$this->DB_CodePageMark = ord(fread($this->FileHandle, 1));
fseek($this->FileHandle, 2, SEEK_CUR); // Ignoring next 2 "reserved" bytes

// Now reading field captions and attributes
while(!feof($this->FileHandle)) {

// Checking for end of header
if(ord(fread($this->FileHandle, 1)) == 13) {
break; // End of header!
} else {
// Go back
fseek($this->FileHandle, -1, SEEK_CUR);
}

$Field["Name"] = trim(fread($this->FileHandle, 11));
$Field["Type"] = fread($this->FileHandle, 1);
fseek($this->FileHandle, 4, SEEK_CUR); // Skipping attribute "displacement"
$Field["Size"] = ord(fread($this->FileHandle, 1));
fseek($this->FileHandle, 15, SEEK_CUR); // Skipping any remaining attributes
$this->DB_Fields[] = $Field;
}

// Setting file pointer to the first record
fseek($this->FileHandle, $this->DB_FirstData);

$this->FileOpened = true;

// Open memo file, if exists
if(!empty($MemoFilename) and file_exists($MemoFilename) and preg_match("%^(.+).fpt$%i", $MemoFilename)) {
$this->Memo_Handle = fopen($MemoFilename, "r");
if($this->Memo_Handle) {
$this->Memo_Opened = true;

// Getting block size
fseek($this->Memo_Handle, 6);
$Data = unpack("n", fread($this->Memo_Handle, 2));
$this->Memo_BlockSize = $Data[1];
}
}
}

return $Return;
}

public function GetNextRecord($FieldCaptions = false) {
$Return = NULL;
$Record = array();

if(!$this->FileOpened) {
$Return = false;
} elseif(feof($this->FileHandle)) {
$Return = NULL;
} else {
// File open and not EOF
fseek($this->FileHandle, 1, SEEK_CUR); // Ignoring DELETE flag
foreach($this->DB_Fields as $Field) {
$RawData = fread($this->FileHandle, $Field["Size"]);
// Checking for memo reference
if($Field["Type"] == "M" and $Field["Size"] == 4 and !empty($RawData)) {
// Binary Memo reference
$Memo_BO = unpack("V", $RawData);
if($this->Memo_Opened and $Memo_BO != 0) {
fseek($this->Memo_Handle, $Memo_BO[1] * $this->Memo_BlockSize);
$Type = unpack("N", fread($this->Memo_Handle, 4));
if($Type[1] == "1") {
$Len = unpack("N", fread($this->Memo_Handle, 4));
$Value = trim(fread($this->Memo_Handle, $Len[1]));
} else {
// Pictures will not be shown
$Value = "{BINARY_PICTURE}";
}
} else {
$Value = "{NO_MEMO_FILE_OPEN}";
}
} else {
$Value = trim($RawData);
}

if($FieldCaptions) {
$Record[$Field["Name"]] = $Value;
} else {
$Record[] = $Value;
}
}

$Return = $Record;
}

return $Return;
}

function __destruct() {
// Cleanly close any open files before destruction
$this->Initialize();
}
}

类可以这样使用:

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

这可能不是一个万能的完美类(class),但它对我有用。随意使用这段代码,但请注意该类非常宽容 - 它不关心 fread() 和 fseek() 是否返回 true 或其他任何东西 - 所以你可能想在使用前稍微改进它。

另请注意,还有许多私有(private)变量,如记录数、记录大小等目前未使用。

关于php - 如何用 PHP 阅读 FoxPro 备忘录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1947348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com