faustvik/text-files

PHP library for reading, writing and processing text files and CSV with a clean contract-based API

Maintainers

Package info

github.com/FaustVik/text-files

pkg:composer/faustvik/text-files

Transparency log

Statistics

Installs: 3

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.2.0 2026-08-05 15:02 UTC

This package is auto-updated.

Last update: 2026-08-05 15:05:31 UTC


README

PHP library for reading, writing and processing text files and CSV.

Installation

composer require faustvik/text-files

Requirements: PHP >= 8.1, ext-json Optional: ext-posix (for FileInfo::getFileOwner())

Quick Start

Text Files

use FaustVik\Files\Text\TextFileManager;

$text = TextFileManager::fromPath('/path/to/file.txt');

// Read
$lines = $text->readToArray();
$content = $text->readToString();
$count = $text->count();

// Write
$text->write('appended content');
$text->overWrite('new content');
$text->appendToStartFile('prepended content');

// Search & modify
$found = $text->search('keyword');          // case-insensitive
$text->replaceLine(2, 'new line content');
$text->deleteLine(0);

// Memory-efficient iteration
foreach ($text->lines() as $index => $line) {
    echo $line;
}

Skip empty lines:

$text = TextFileManager::fromPath('/path/to/file.txt', skipEmptyLines: true);

CSV Files

use FaustVik\Files\Csv\CsvManager;

$csv = CsvManager::fromPath('/path/to/data.csv');

// Read
$data = $csv->read();
$headers = $csv->getHeadersColumn();
$count = $csv->count();

// Write
$csv->write([['id' => 1, 'name' => 'Alice', 'age' => 30]]);
$csv->overWrite($allData);

// Row operations
$csv->insertLine(['3', 'Charlie', '35'], 1);
$csv->deleteLine([0, 2]);

// Column operations
$csv->addColumn('status', 'active');
$csv->addColumn('id', '0', position: 0);   // insert at specific position
$csv->deleteColumn([1]);
$csv->updateHeaders(['ID', 'Full Name', 'Age']);
$csv->getColumns([0, 2]);

// Filter & search
$csv->filterByColumn(2, '30');
$csv->filter(fn(array $row): bool => (int) $row[2] > 25);
$csv->search('Alice');
$csv->search('New York', columnIndex: 2);

// Transform
$names = $csv->map(fn(array $row): string => $row[1]);

// Streaming (memory-efficient)
$csv->stream(function (array $row): ?bool {
    echo implode(', ', $row) . "\n";
    return true; // return false to stop
});

$csv->chunk(100, function (array $rows): ?bool {
    processRows($rows);
    return true;
});

CSV settings:

$csv = CsvManager::fromPath(
    path: '/path/to/data.csv',
    separator: ';',
    skipFirstLine: true,
    escapeChar: '\\',
    enclosureChar: '"',
);

Helpers

Standalone utility classes for common file system operations.

FileInfo

use FaustVik\Files\Helpers\File\FileInfo;

FileInfo::getName('/path/to/file.txt');     // 'file.txt'
FileInfo::getSize('/path/to/file.txt');     // size in bytes
FileInfo::getExtension('/path/to/file.txt'); // 'txt'
FileInfo::isReadable('/path/to/file.txt');  // true/false
FileInfo::isWritable('/path/to/file.txt');  // true/false
FileInfo::isFile('/path/to/file.txt');      // true/false
FileInfo::getFileTime('/path/to/file.txt'); // modification timestamp
FileInfo::getFileOwner('/path/to/file.txt'); // requires ext-posix

FileOperation

use FaustVik\Files\Helpers\File\FileOperation;

FileOperation::copy('/from.txt', '/to.txt');
FileOperation::move('/from.txt', '/to.txt');
FileOperation::rename('/path/to/file.txt', 'newname.txt');
FileOperation::delete('/path/to/file.txt');
FileOperation::flush('/path/to/file.txt');  // clear file content

DirectoryInfo & DirectoryOperation

use FaustVik\Files\Helpers\Directory\DirectoryInfo;
use FaustVik\Files\Helpers\Directory\DirectoryOperation;
use FaustVik\Files\Helpers\Directory\EnumSortDirectory;

DirectoryInfo::scan('/path/to/dir', EnumSortDirectory::Asc);
DirectoryInfo::isDirExist('/path/to/dir');

DirectoryOperation::createDir('/path/to/dir', recursive: true);
DirectoryOperation::deleteDir('/path/to/dir');

Exceptions

All exceptions extend BaseTextFileException:

  • FileNotFoundException — file does not exist
  • FileIsNotReadableException — file is not readable
  • FileIsNotWriteableException — file is not writable
  • FileExtensionIsNotSupportedException — unsupported file extension (e.g. non-CSV)
  • CantReadFileException — read operation failed
  • IsNotResourceException — expected a resource, got something else
  • DirectoryExceptionBase — directory operation failed

License

MIT