PHP Classes

File: example-with-fixed-header.php

Recommend this page to a friend!
  Classes of Christian Vigh   Random Access File   example-with-fixed-header.php   Download  
File: example-with-fixed-header.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Random Access File
Store data fixed record length data in files
Author: By
Last change:
Date: 8 years ago
Size: 1,265 bytes
 

Contents

Class file image Download
<?php
   
/***
        This example opens a file (random-with-fixed-header.dat) having the following contents :

            This is a fixed-length header!Record01Record02Record03

        The string "This is a fixed-length header!" is the header
        It contains 3 records, "Record01" through "Record03", of length 8.

        The script then displays :
        - The header contents
        - The number of records
        - Each individual record
     ***/
   
require ( "RandomAccessFile.phpclass" ) ;

    if (
php_sapi_name ( ) != 'cli' )
        echo
"<pre>" ;

   
$random_file = "random-with-fixed-header.dat" ;

   
// Open the random access file of record size 8
   
$rf = new RandomAccessFile ( $random_file, 8 ) ;

   
// Set the header size
    // Note that we could have specified this value as the 5th parameter of the constructor
    // but we did not want to specify values for the $cache_size and $filler parameter, since
    // we're simply opening the file in read-only mode
   
$rf -> HeaderSize = 30 ;
                   
   
// Open the file in read-only mode
   
$rf -> Open ( true ) ;

   
// Display data
   
echo "HEADER DATA : [{$rf -> Header}]\n" ;
    echo
"RECORD COUNT : " . count ( $rf ) . "\n" ;
   
    for (
$i = 0 ; $i < count ( $rf ) ; $i ++ )
        echo
"RECORD #" . ( $i + 1 ) . " : [{$rf [$i]}]\n" ;