totalcrm/php-dbf
Composer 安装命令:
composer require totalcrm/php-dbf
包简介
A simple parser for *.dbf files using PHP
README 文档
README
A simple library for dealing with dbf databases like dBase and FoxPro. It's a port of PHPXbase class written by Erwin Kooi, updated to a PSR-2 compliant code and tweaked for performance and to solve some issues the original code had.
Installation
You can install it through Composer:
$ composer require totalcrm/php-dbf
Sample usage
More samples in examle folder.
Reading data
use TotalCRM\DBase\TableReader; $table = new TableReader('test.dbf'); while ($record = $table->nextRecord()) { echo $record->get('my_column'); //or echo $record->my_column; }
If the data in DB is not in UTF-8 you can specify a charset to convert the data from:
use TotalCRM\DBase\TableReader; $table = new TableReader( 'test.dbf', [ 'encoding' => 'cp1251' ] );
It is also possible to read Memos from dedicated files. Just make sure that .fpt file with the same name as main database exists.
Performance
You can pass an array of the columns that you need to the constructor, then if your table has columns that you don't use they will not be loaded. This way the parser can run a lot faster.
use TotalCRM\DBase\TableReader; $table = new TableReader( 'test.dbf', [ 'columns' => ['my_column', 'another_column'] ] ); while ($record = $table->nextRecord()) { echo $record->my_column; echo $record->another_column; }
If you know the column type already, you can also call the type-specific function for that field, which increases the speed too.
while ($record = $table->nextRecord()) { echo $record->get('my_column'); echo $record->get('another_column'); }
Editing Data
To open a table for editing, you have to use a TableEditor object, as on this example:
use TotalCRM\DBase\TableEditor; $table = new TableEditor('test.dbf'); for ($i = 0; $i < 10; $i++) { $record = $table->nextRecord(); $record->set('field', 'string'); //or $record->field = 'string'; $table->writeRecord(); } $table ->save() ->close();
Add new record
use TotalCRM\DBase\TableEditor; $table = new TableEditor( 'file.dbf', [ 'editMode' => TableEditor::EDIT_MODE_CLONE, //default ] ); $record = $table->appendRecord(); $record->set('name', 'test name'); $record->set('age', 20); $table ->writeRecord() ->save() ->close();
Delete record
use TotalCRM\DBase\TableEditor; $table = new TableEditor('file.dbf'); while ($record = $table->nextRecord()) { if ($record->get('delete_this_row')) { $table->deleteRecord(); //mark record deleted } } $table ->pack() //remove deleted rows ->save() //save changes ->close();
Creating table
To create a table file you need to use the TableCreator object.
use TotalCRM\DBase\Enum\FieldType; use TotalCRM\DBase\Enum\TableType; use TotalCRM\DBase\Header\Column; use TotalCRM\DBase\Header\HeaderFactory; use TotalCRM\DBase\TableCreator; use TotalCRM\DBase\TableEditor; // you can specify any other database version from TableType $header = HeaderFactory::create(TableType::DBASE_III_PLUS_MEMO); $filepath = '/path/to/new/file.dbf'; $tableCreator = new TableCreator($filepath, $header); $tableCreator ->addColumn(new Column([ 'name' => 'name', 'type' => FieldType::CHAR, 'length' => 20, ])) ->addColumn(new Column([ 'name' => 'birthday', 'type' => FieldType::DATE, ])) ->addColumn(new Column([ 'name' => 'is_man', 'type' => FieldType::LOGICAL, ])) ->addColumn(new Column([ 'name' => 'bio', 'type' => FieldType::MEMO, ])) ->addColumn(new Column([ 'name' => 'money', 'type' => FieldType::NUMERIC, 'length' => 20, 'decimalCount' => 4, ])) ->addColumn(new Column([ 'name' => 'image', 'type' => FieldType::MEMO, ])) ->save(); //creates file $table = new TableEditor($filepath); //... add records
Troubleshooting
I'm not an expert on dBase and I don't know all the specifics of the field types and versions, so the lib may not be able to handle some situations. If you find an error, please open an issue and send me a sample table that I can reproduce your problem, and I'll try to help.
Useful links
totalcrm/php-dbf 适用场景与选型建议
totalcrm/php-dbf 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24.23k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2021 年 12 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「dbase」 「dbf」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 totalcrm/php-dbf 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 totalcrm/php-dbf 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 totalcrm/php-dbf 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Library to access dbase / xbase / dbf database files
A PHP library to parse ESRI Shape files
Package for reading DBASE-files (FoxPro) with/without MEMO-fields
A simple parser for *.dbf, *.fpt files using PHP
PHP library to read and write ESRI Shapefiles, compatible with WKT and GeoJSON
Package for converting DBASE-files to MySQL
统计信息
- 总下载量: 24.23k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-12-02