<?php

function main(){

  // start html-code
  echo '<html><head><title>FWrite Test</title></head><body>'."\n";


  // if 'testfile.txt' exists, we try to write in the middle of it.
  // the result can be viewed with a simple texteditor like notepad or so.

  if ( file_exists( 'testfile.txt' ) ):

    $FileHandle = fopen( 'testfile.txt','a+' );                    // open the file
    fseek( $FileHandle ,5 );                                       // position the filepointer
    $Pos = ftell( $FileHandle );                                   // verify its position
    echo 'Position of filepointer: ' . $Pos . '.<br>'."\n";        // and tell us

    $Text = 'abcdefghijklmnop';                                    // create some text
    fwrite( $FileHandle ,$Text ,16 );                              // write it to the file
    fclose( $FileHandle );                                         // close the file
    echo 'Existing testfile changed.<br>'."\n";


  // if there is no file called 'testfile.txt', then we create it.
  else:

    $FileHandle = fopen( 'testfile.txt','w+' );         // create the file
    $Text = 'This piece of text is 36 bytes long.';     // create some text
    fwrite( $FileHandle ,$Text ,36 );                   // write it to the file
    fclose( $FileHandle );                              // close the file

    echo 'New testfile created.<br>'."\n";

  endif;


  // end html-code
  echo '</body></html>'."\n";


}

main();

?>
