<?php // example 01 : simple open and close
require_once( "cfile.class.php" );
echo "<b>EXAMPLE 03</b>: creating a file, then we write in a string<br><br>" ;
$CANDIDATEfile = "cfile.test.example03.xxx" ;
$cfile = new cfile( $CANDIDATEfile );
$bOPEN = $cfile->open( CFILE_READWRITE_CREATE_MODE );
$bERR = $cfile->is_error() ;
if ( $bOPEN && !$bERR ) // you can check open return value or internal error for safe operation
{
echo "OPEN FILE <b>$CANDIDATEfile</b> : SUCCESS<br>" ;
echo "FILE SIZE.".( filesize( $CANDIDATEfile ) )."<br>" ;
echo "<br>" ;
echo "Writing a simple string in text mode ...<br>" ;
$STR = " this is some string" ;
$bWRITE = $cfile->write( $STR, strlen($STR), CFILE_TEXT_MODE ) ;
echo ( $bWRITE ) ? "WRITE some string: <b>$STR</b><br>" : "CAN'T WRITE some string ...<br>" ;
echo "<br>Now read something ...<br><br>" ;
echo "... try to move to beginning of the file<br>" ;
$bBEGIN = $cfile->move_to_beginning();
echo ( $bWRITE ) ? "OK MOVE TO BEGINNING ...<br>" : "CAN'T MOVE TO THE BEGINNING ...<br>" ;
// and read the next bytes
$nbytes = strlen($STR) ;
$READ = $cfile->read( $nbytes, CFILE_TEXT_MODE ) ;
echo ( $READ === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "This is what I read in $nbytes byte".( ( $nbytes == 1 ) ? "" : "s" )." : <b>$READ</b><br>" ;
echo "<br>" ;
echo ( $cfile->close() ) ? "CLOSE FILE <b>$CANDIDATEfile</b> : SUCCESS" : $cfile->get_error_string() ;
}
else echo $cfile->get_error_string() ;
?>
|