{"id":18999891,"url":"https://github.com/totalcrm/php-dbf","last_synced_at":"2025-04-22T16:52:40.440Z","repository":{"id":57072020,"uuid":"434149381","full_name":"totalcrm/php-dbf","owner":"totalcrm","description":"PHP library dBase and FoxPro format read and write by TotalCRM","archived":false,"fork":false,"pushed_at":"2022-04-23T09:29:14.000Z","size":55,"stargazers_count":9,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-14T09:53:09.863Z","etag":null,"topics":["dbase","foxpro","foxpro-dbf-files","php","php-library","totalcrm"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/totalcrm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-12-02T09:00:39.000Z","updated_at":"2024-07-11T14:41:09.000Z","dependencies_parsed_at":"2022-08-24T10:52:16.117Z","dependency_job_id":null,"html_url":"https://github.com/totalcrm/php-dbf","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/totalcrm%2Fphp-dbf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/totalcrm%2Fphp-dbf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/totalcrm%2Fphp-dbf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/totalcrm%2Fphp-dbf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/totalcrm","download_url":"https://codeload.github.com/totalcrm/php-dbf/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250282985,"owners_count":21405075,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dbase","foxpro","foxpro-dbf-files","php","php-library","totalcrm"],"created_at":"2024-11-08T18:05:38.381Z","updated_at":"2025-04-22T16:52:40.413Z","avatar_url":"https://github.com/totalcrm.png","language":"PHP","readme":"# PHP DBase\n\nA simple library for dealing with **dbf** databases like dBase and FoxPro. It's a port of PHPXbase class written by [Erwin Kooi](http://www.phpclasses.org/package/2673-PHP-Access-dbf-foxpro-files-without-PHP-ext-.html), updated to a PSR-2 compliant code and tweaked for performance and to solve some issues the original code had.\n\n\n## Installation\n\nYou can install it through [Composer](https://getcomposer.org):\n\n```shell script\n$ composer require totalcrm/php-dbf\n```\n\n\n## Sample usage\n\nMore samples in `examle` folder.\n\n\n### Reading data\n\n```php\nuse TotalCRM\\DBase\\TableReader;\n\n$table = new TableReader('test.dbf');\n\nwhile ($record = $table-\u003enextRecord()) {\n    echo $record-\u003eget('my_column');\n    //or\n    echo $record-\u003emy_column;\n}\n```\n\nIf the data in DB is not in UTF-8 you can specify a charset to convert the data from:\n\n```php\nuse TotalCRM\\DBase\\TableReader;\n\n$table = new TableReader(\n    'test.dbf',\n    [\n        'encoding' =\u003e 'cp1251'\n    ]\n);\n```\n\nIt is also possible to read Memos from dedicated files. Just make sure that *.fpt* file with the same name as main database exists.\n\n\n#### Performance\n\nYou 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. \nThis way the parser can run a lot faster.\n\n```php\nuse TotalCRM\\DBase\\TableReader;\n\n$table = new TableReader(\n    'test.dbf', \n    [\n        'columns' =\u003e ['my_column', 'another_column']\n    ]\n);\n\nwhile ($record = $table-\u003enextRecord()) {\n    echo $record-\u003emy_column;\n    echo $record-\u003eanother_column;\n}\n```\n\nIf you know the column type already, you can also call the type-specific function for that field, which increases the speed too.\n\n```php\nwhile ($record = $table-\u003enextRecord()) {\n    echo $record-\u003eget('my_column');\n    echo $record-\u003eget('another_column');\n}\n```\n\n\n### Editing Data\n\nTo open a table for editing, you have to use a `TableEditor` object, as on this example:\n\n```php\nuse TotalCRM\\DBase\\TableEditor;\n\n$table = new TableEditor('test.dbf');\n\nfor ($i = 0; $i \u003c 10; $i++) {\n    $record = $table-\u003enextRecord();\n    \n    $record-\u003eset('field', 'string');\n    //or\n    $record-\u003efield = 'string';\n\n    $table-\u003ewriteRecord();\n}\n\n$table\n    -\u003esave()\n    -\u003eclose();\n```\n\n\n#### Add new record\n\n```php\nuse TotalCRM\\DBase\\TableEditor;\n\n$table = new TableEditor(\n    'file.dbf',\n    [\n        'editMode' =\u003e TableEditor::EDIT_MODE_CLONE, //default\n    ]\n);\n$record = $table-\u003eappendRecord();\n$record-\u003eset('name', 'test name');\n$record-\u003eset('age', 20);\n\n$table\n    -\u003ewriteRecord()\n    -\u003esave()\n    -\u003eclose();\n```\n\n\n#### Delete record\n\n```php\nuse TotalCRM\\DBase\\TableEditor;\n\n$table = new TableEditor('file.dbf');\n\nwhile ($record = $table-\u003enextRecord()) {\n    if ($record-\u003eget('delete_this_row')) {\n        $table-\u003edeleteRecord(); //mark record deleted\n    }    \n}\n\n$table\n    -\u003epack() //remove deleted rows\n    -\u003esave() //save changes\n    -\u003eclose();\n```\n\n### Creating table\n\nTo create a table file you need to use the `TableCreator` object.\n\n```php\nuse TotalCRM\\DBase\\Enum\\FieldType;\nuse TotalCRM\\DBase\\Enum\\TableType;\nuse TotalCRM\\DBase\\Header\\Column;\nuse TotalCRM\\DBase\\Header\\HeaderFactory;\nuse TotalCRM\\DBase\\TableCreator;\nuse TotalCRM\\DBase\\TableEditor;\n\n// you can specify any other database version from TableType\n$header = HeaderFactory::create(TableType::DBASE_III_PLUS_MEMO);\n$filepath = '/path/to/new/file.dbf';\n\n$tableCreator = new TableCreator($filepath, $header);\n$tableCreator\n    -\u003eaddColumn(new Column([\n        'name'   =\u003e 'name',\n        'type'   =\u003e FieldType::CHAR,\n        'length' =\u003e 20,\n    ]))\n    -\u003eaddColumn(new Column([\n        'name'   =\u003e 'birthday',\n        'type'   =\u003e FieldType::DATE,\n    ]))\n    -\u003eaddColumn(new Column([\n        'name'   =\u003e 'is_man',\n        'type'   =\u003e FieldType::LOGICAL,\n    ]))\n    -\u003eaddColumn(new Column([\n        'name'   =\u003e 'bio',\n        'type'   =\u003e FieldType::MEMO,\n    ]))\n    -\u003eaddColumn(new Column([\n        'name'         =\u003e 'money',\n        'type'         =\u003e FieldType::NUMERIC,\n        'length'       =\u003e 20,\n        'decimalCount' =\u003e 4,\n    ]))\n    -\u003eaddColumn(new Column([\n        'name'   =\u003e 'image',\n        'type'   =\u003e FieldType::MEMO,\n    ]))\n    -\u003esave(); //creates file\n\n$table = new TableEditor($filepath);\n//... add records \n```\n\n\n## Troubleshooting\n\nI'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. \nIf 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.\n\n\n## Useful links\n\n[Xbase File Format Description](http://www.manmrk.net/tutorials/database/xbase/)\n\n[File Structure for dBASE 7](http://www.dbase.com/KnowledgeBase/int/db7_file_fmt.htm)\n\n[DBF AND DBT/FPT FILE STRUCTURE](http://www.independent-software.com/dbase-dbf-dbt-file-format.html)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftotalcrm%2Fphp-dbf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftotalcrm%2Fphp-dbf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftotalcrm%2Fphp-dbf/lists"}