2017 © Pedro PelĆ”ez
 

library php-xbase

A simple parser for *.dbf, *.fpt files using PHP

image

hisamu/php-xbase

A simple parser for *.dbf, *.fpt files using PHP

  • Sunday, May 20, 2018
  • by lsouza
  • Repository
  • 20 Watchers
  • 87 Stars
  • 61,947 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 51 Forks
  • 24 Open issues
  • 7 Versions
  • 10 % Grown

The README.md

PHP XBase

Build Status Test Coverage Latest Stable Version Total Downloads License, (*1)

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., (*2)

Installation

You can install it through Composer:, (*3)

```shell script $ composer require hisamu/php-xbase, (*4)



## Sample usage More samples in `tests` folder. ### Reading data ```php use XBase\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:, (*5)

use XBase\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., (*6)

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., (*7)

use XBase\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., (*8)

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:, (*9)

use XBase\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 XBase\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 XBase\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., (*10)

use XBase\Enum\FieldType;
use XBase\Enum\TableType;
use XBase\Header\Column;
use XBase\Header\HeaderFactory;
use XBase\TableCreator;
use XBase\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., (*11)

Xbase File Format Description, (*12)

File Structure for dBASE 7, (*13)

DBF AND DBT/FPT FILE STRUCTURE, (*14)

The Versions

20/05 2018

dev-master

9999999-dev https://github.com/luads/php-xbase

A simple parser for *.dbf, *.fpt files using PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by LuĆ£ de Souza

dbase foxpro

09/01 2017

1.0.5

1.0.5.0 https://github.com/hisamu/php-xbase

A simple parser for *.dbf, *.fpt files using PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by LuĆ£ de Souza

dbase foxpro

05/01 2017

1.0.4

1.0.4.0 https://github.com/hisamu/php-xbase

A simple parser for *.dbf, *.fpt files using PHP

  Sources   Download

The Requires

  • php >=5.3.0

 

by LuĆ£ de Souza

dbase foxpro

14/12 2016

1.0.3

1.0.3.0 https://github.com/hisamu/php-xbase

A simple parser for *.dbf, *.fpt files using PHP

  Sources   Download

The Requires

  • php >=5.3.0

 

by LuĆ£ de Souza

dbase foxpro

07/11 2016

1.0.2

1.0.2.0 https://github.com/hisamu/php-xbase

A simple parser for *.dbf, *.fpt files using PHP

  Sources   Download

The Requires

  • php >=5.3.0

 

by LuĆ£ de Souza

dbase foxpro

19/04 2016

1.0.1

1.0.1.0 https://github.com/hisamu/php-xbase

A simple parser for *.dbf files using PHP

  Sources   Download

The Requires

  • php >=5.3.0

 

by LuĆ£ de Souza

dbase foxpro

13/04 2016

1.0.0

1.0.0.0 https://github.com/hisamu/php-xbase

A simple parser for *.dbf files using PHP

  Sources   Download

The Requires

  • php >=5.3.0

 

by LuĆ£ de Souza

dbase foxpro