PHP Classes

Random Access File: Store data fixed record length data in files

Recommend this page to a friend!
  Info   View files Example   View files View files (10)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 207 All time: 8,373 This week: 137Up
Version License PHP version Categories
random-access-file 1.2BSD License5.5PHP 5, Databases, Files and Folders
Description 

Author

This class can store data fixed record length data in files.

It can open files for reading or writing and store records of data of fixed length in them, like those used by database file managers.

The class can also perform other operations such as copy one set of records to another position in the same file, swap or truncate records.

It can handle random access files with fixed-length or variable-length header at the beginning of the file (a variable-length header is a header whose real size is specified in some fixed part of the header itself and therefore must be retrieved when the file is opened).

The class implements array and iterator interfaces, so its objects can be accessed as if they were arrays.

Innovation Award
PHP Programming Innovation award nominee
May 2016
Number 14
Database systems use files that have records of fixed length because it is easy to predict the position of a given record in the file and jump to that position to read or write the record data.

This class can manipulate files with fixed length records in pure PHP code. It performs several types of operations with data records that can be used a simple PHP based file database system.

Manuel Lemos
Picture of Christian Vigh
  Performance   Level  
Name: Christian Vigh <contact>
Classes: 32 packages by
Country: France France
Age: 57
All time rank: 13810 in France France
Week rank: 10 Up1 in France France Up
Innovation award
Innovation award
Nominee: 20x

Winner: 3x

Example

<?php
   
require ( "RandomAccessFile.phpclass" ) ;

   
$random_file = "random.dat" ;

   
// Initialize a file containing the numbers 0 through 100, written with 3 digits
    // and terminated by a newline (each record will occupy 4 bytes)
    // This file will be used as our example case for testing random file access.
   
$fp = fopen ( $random_file, "w" ) ;

    for (
$i = 1 ; $i < 100 ; $i ++ )
       
fwrite ( $fp, sprintf ( "%03d", $i ) . "\n" ) ;

   
fwrite ( $fp, "100" ) ; // Note that the last record will be incomplete (no terminating newline)
   
fclose ( $fp ) ;

   
// Instantiate a random access file and open it in read/write mode.
    // "4" is the record size, "1024" the number of records to be cached, and "\n" the filler character to be
    // used when inserting empty records
   
$rf = new RandomAccessFile ( $random_file, 4, 0, 1024, "\n" ) ;
   
$rf -> Open ( ) ;

   
// Show the number of records that this file holds (should be 100)
   
echo ( "Count = " . count ( $rf ) . "\n" ) ;

   
// Swap 10 records (3d parameter) from record #0 with record #10
    // The file should now have the following values (one per line) :
    // Records 0 to 9 : 011..020
    // Records 10 to 19 : 001..010
    // Records 20 to 99 : 021..099
   
$rf -> Swap ( 0, 10, 10 ) ;

   
// Now copy 20 records from record #0 to record #100 (which is past the end of file)
    // The new contents should have 20 more records, with values in the range 011..020 and 001..010
   
$rf -> Copy ( 0, 100, 20 ) ;

   
// Note that you can use the for() and foreach() constructs to loop through each record
   
foreach ( $rf as $entry )
        echo (
"[" . trim ( $entry ) . "]\n") ;

   
// There is also a small (and dumb) cache that store a few statistics
   
echo ( "Hits : {$rf -> CacheHits}, misses = {$rf -> CacheMisses}\n" ) ;


  Files folder image Files  
File Role Description
Accessible without login Plain text file example-with-fixed-header.php Example Example script
Accessible without login Plain text file example-with-variable-header.php Example Example script
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file NOTICE Data Auxiliary data
Accessible without login Plain text file random-with-fixed-header.dat Data Auxiliary data
Accessible without login Plain text file random-with-variable-header.dat Data Auxiliary data
Accessible without login Plain text file random.dat Data Auxiliary data
Plain text file RandomAccessFile.phpclass Class Class source
Accessible without login Plain text file README.md Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:207
This week:0
All time:8,373
This week:137Up