File class

From PhiWiki
Jump to: navigation, search

The file class contains functions and data related to file access. You can even translate text files to different codecs.

Usage

The file class is a global object in the server scripting environment and is based on handles.

loadModule('file');
var handle=file.open( file.tempdir+'/myfile', file.writeonly+file.append );
if ( handle ) {
 file.write( handle, 'textdata' ); // appends textdata as utf-8 stream to myfile
 file.close( handle );
}

Properties

  • Number append [read]
  • String lasterror [read]
  • Number readonly [read]
  • Number readwrite [read]
  • StringArray properties [read]
  • String tempdir [read]
  • Number writeonly [read]

Number append

Append flag to open the file in append mode.

var handle=file.open( 'myfile', file.writeonly+file.append );

String lasterror

Returns the last error occured or an empty string if there is no error.

var error=file.lasterror;

See also: file.error().

Number readonly

Read only flag to open the file in read only mode.

var handle=file.open( 'myfile', file.readonly );

Number readwrite

Read/write flag to open the file in read and write mode.

var handle=file.open( 'myfile', file.readwrite );

StringArray properties

Returns the available properties for the file class in a string array.

var i=0;
while ( file.properties[i] ) print( file.properties[i++] );

String tempdir

Returns the systems temporary directory. Usually this is /tmp on Unix and C:\temp on Windows. Actually it is not the temp directory as of the Server class where the server stores its runtime objects.

var tmp=file.tempdir;

See also: server.tempdir, $PHISERVER[tempdir].

Number writeonly

Write only flag to open the file in write only mode.

var handle=file.open( 'myfile', file.writeonly );

See also: file.append.

Functions

Boolean atEnd()

Returns true if the file with handle is at the end.

if ( file.atEnd( handle ) ) doSomething();

Void close()

Closes the file with handle and releases the file handle.

file.close( handle );

Boolean copy()

Copies a file and returns true on success.

file.copy( 'source', 'destination' );

String error()

Returns the error string for handle or an empty string if no error occured.

var error=file.error( handle );

See also: file.lasterror.

Boolean exists()

Returns true if filename exists.

if ( file.exists( 'filename' ) ) doSomething();

Boolean isOpen()

Returns true if the file with handle is open or false if not.

if ( file.isOpen( handle ) ) doSomething();

Number open()

Opens the file filename in mode openmode and returns the new handle on success or '0' if an error occured.

var handle=file.open( 'myfile', openmode );

The openmode can be file.readonly, file.writeonly and file.readwrite. If you want data appended to the file use:

var handle=file.open( file.tempdir+'/myfile', file.writeonly+file.append );

See also: file.lasterror.

String read()

Reads from file with handle and returns the string or an empty string if any error occured.

read( handle, codec='utf-8' );

Example:

var content=file.read( handle ); //reads text from file and asumes utf-8 as text codec.

Boolean remove()

Removes the file with filename and returns true on success.

if ( file.remove( 'filename' ) ) doSomething();

Boolean write()

Writes stringdata to the file with handle and returns true on success or false if an error occured.

file.write( handle, stringdata, codec='utf-8' );

Example:

if ( file.write( handle, 'my string' ) ) {
 //success
} else {
 print( file.error( handle ) ); //prints error to stdout
}

Additional notes

You may use different codecs to translate the text written or read by file. If you overwrite the codec with an empty string Phi will perform a system call to get the codec for the current locale the server is running on.