ClearFoundation

ClearOS API

Class File

Description

File handling class.

The File class can be use for creating, reading and manipulating the contents of a file. If you need to change a configuration file, this may be the class for you. However, configuration files come in many different forms, so this might not have what you need. Feel free to do your own file parsing.

  • author: Point Clark Networks
  • copyright: Copyright 2006, Point Clark Networks
  • license: GNU

Located in /api/File.class.php (line 215)

Engine
   |
   --File
Direct descendents
Class Description
ConfigurationFile Configuration file handling class.
Class Constant Summary
 CMD_CAT = '/bin/cat'
 CMD_CHMOD = '/bin/chmod'
 CMD_CHOWN = '/bin/chown'
 CMD_COPY = '/bin/cp'
 CMD_FILE = '/usr/bin/file'
 CMD_HEAD = '/usr/bin/head'
 CMD_LS = '/bin/ls'
 CMD_MD5 = '/usr/bin/md5sum'
 CMD_MOVE = '/bin/mv'
 CMD_REPLACE = '/usr/sbin/app-rename'
 CMD_RM = '/bin/rm'
 CMD_TOUCH = '/bin/touch'
Variable Summary
boolean $contents
string $filename
superuser $superuser
boolean $temporary
Method Summary
void AddLines (string $data)
void AddLinesAfter (string $data, string $after)
void AddLinesBefore (string $data, string $before)
void Chmod (string $mode)
void Chown (string $owner, string $group)
File __construct (string $filename, [boolean $superuser = false], [boolean $temporary = false])
void CopyTo (string $destination)
void Create (string $owner, string $group, string $mode)
void Delete ()
integer DeleteLines (string $search)
void DumpContentsFromArray (array $contents)
boolean Exists ()
string GetContents ([int $maxbytes = -1])
array GetContentsAsArray ([int $maxbytes = -1])
string GetFilename ()
string GetMd5 ()
string GetPermissions ()
array GetSearchResults (string $regex, [int $maxbytes = -1])
int GetSize ()
boolean IsDirectory ()
integer IsSymLink ()
long LastModified ()
string LookupLine (string $search)
string LookupValue (string $key)
string LookupValueBetween (string $key, string $start, string $end)
void MoveTo (string $destination)
boolean PrependLines (string $search, string $prepend)
void Replace (string $tempfile)
integer ReplaceLines (string $search, string $replacement, [integer $maxreplaced = -1])
void ReplaceLinesBetween (string $search, string $replacement, string $start, string $end)
boolean ReplaceLinesByPattern (string $search, string $replacement)
integer ReplaceOneLine (string $search, string $replacement)
boolean ReplaceOneLineByPattern (string $search, string $replacement)
Variables
boolean $contents = null (line 243)
  • var: contents loaded flag
  • access: protected
string $filename = null (line 225)
  • var: filename
  • access: protected
superuser $superuser = false (line 231)
  • var: superuser
  • access: protected
boolean $temporary = false (line 237)
  • var: temporary Temporary file
  • access: protected

Inherited Variables

Inherited from Engine

Engine::$errors
Methods
AddLines (line 921)

Appends data to a file.

The method does not automatically add a newline - that is up to you!

  • throws: FileNotFoundException FileException
void AddLines (string $data)
  • string $data: line (or lines) to append to the file
AddLinesAfter (line 961)

Appends a line (or lines) to a file at a particular location in the file.

  • throws: FileNoMatchException, FileNotFoundException, FileException
void AddLinesAfter (string $data, string $after)
  • string $data: line(s) to insert into file
  • string $after: regular expression defining the file location
AddLinesBefore (line 1005)

Prepends a line (or lines) to a file at a particular location in the file.

  • throws: FileNoMatchException, FileNotFoundException, FileException
void AddLinesBefore (string $data, string $before)
  • string $data: line(s) to insert into file
  • string $before: regular expression defining the file location
Chmod (line 554)

Changes file mode.

  • throws: ValidationException, FileNotFoundException, FilePermissionsException, FileException
void Chmod (string $mode)
  • string $mode: mode of the file
Chown (line 587)

Changes file owner and/or group.

Leave the owner or group blank if you do not want change one or the other.

  • throws: ValidationException, FileNotFoundException, FilePermissionsException, FileException
void Chown (string $owner, string $group)
  • string $owner: file owner
  • string $group: file group
Constructor __construct (line 270)

File constructor.

  • access: public
File __construct (string $filename, [boolean $superuser = false], [boolean $temporary = false])
  • boolean $superuser: superuser access required to read the file
  • boolean $temporary: create a temporary file
  • string $filename: filename target file

Redefinition of:
Engine::__construct()
Engine constructor.

Redefined in descendants as:
CopyTo (line 1196)

Copies the file to new location.

  • throws: FileException, ValidationException
void CopyTo (string $destination)
  • string $destination: destination location
Create (line 697)

Creates a file on the system.

  • throws: FileAlreadyExistsException, FilePermissionsException, FileException
void Create (string $owner, string $group, string $mode)
  • string $owner: file owner
  • string $group: file group
  • string $mode: mode of the file
Delete (line 736)

Deletes the file.

  • throws: FileNotFoundException, FileException
void Delete ()
DeleteLines (line 1048)

Removes lines from a file that match the regular expression.

  • return: number of lines deleted
  • throws: FileNotFoundException Exception (inherited from GetContents)
integer DeleteLines (string $search)
  • string $search: regular expression used to match removed lines
DumpContentsFromArray (line 891)

Writes array data to a file.

The method does not automatically add a newline - that is up to you! This method will return an error if the file does not exist.

void DumpContentsFromArray (array $contents)
  • array $contents: an array containing output lines
Exists (line 451)

Checks the existence of the file.

  • return: true if file exists
  • throws: FileException
boolean Exists ()
GetContents (line 308)

Returns the contents of a file.

Set maxbytes to -1 to disable file size limit.

  • return: contents of file
  • throws: FileNotFoundException, FileException
string GetContents ([int $maxbytes = -1])
  • int $maxbytes: maximum number of bytes
GetContentsAsArray (line 331)

Returns the contents of a file in an array.

Set maxbytes to -1 to disable file size limit.

  • return: contents of file
  • throws: FileNotFoundException, FileException
array GetContentsAsArray ([int $maxbytes = -1])
  • int $maxbytes: maximum number of bytes
GetFilename (line 293)

Returns the filename.

  • return: name of file
string GetFilename ()
GetMd5 (line 516)

Returns the MD5 hash of the file.

  • return: the MD5 hash
  • throws: FileException
string GetMd5 ()
GetPermissions (line 632)

Returns the octal permissions of the current file.

  • return: file permissions
  • throws: FileNotFoundException, FileException
string GetPermissions ()
GetSearchResults (line 387)

Returns the contents of a file that match the given regular expression.

Set maxbytes to -1 to disable file size limit.

  • return: contents of file
  • throws: FileNotFoundException, FileException
array GetSearchResults (string $regex, [int $maxbytes = -1])
  • string $regex: search string
  • int $maxbytes: maximum number of bytes
GetSize (line 484)

Returns the file size.

  • return: the file size
  • throws: FileException
int GetSize ()
IsDirectory (line 763)

Checks to see if specified file is a directory.

  • return: true if file is a directory
  • throws: FileException
boolean IsDirectory ()
IsSymLink (line 798)

Checks to see if specified file is a symbolic link.

  • return: 0 if not, 1 if active sym link, -1 if broken sym link
  • throws: FileException
integer IsSymLink ()
LastModified (line 655)

Returns the last modified date of the file.

  • return: representing time file was last modified
  • throws: FileNotFoundException, FileException
long LastModified ()
LookupLine (line 1118)

Searches the file with the given regular expression and returns the first match.

  • return: matching line
  • throws: ValidationException, FileNoMatchException, FileNotFoundException, FileException
string LookupLine (string $search)
  • string $search: regular expression
LookupValue (line 425)

Returns a value for a given unique regular expression.

This method is handy for simple configuration files with key/value pairs. The method will return a FileNoMatchException error if no match was made.

  • return: value for the give key
  • throws: ValidationException, FileNoMatchException, FileNotFoundException, FileException
string LookupValue (string $key)
  • string $key: search string
LookupValueBetween (line 1150)

Similar to LookupValue, except you can specify a subsection of the target file.

The start and end are regular expressions. This can be handy in Apache-style configuration files (e.g. configuring a particular Virtual Host).

  • return: value for the give key
  • throws: FileNotFoundException Exception (inherited from GetContentsAsArray)
  • throws: FileNoMatchException
string LookupValueBetween (string $key, string $start, string $end)
  • string $key: search string
  • string $start: regular expression specifying the start line
  • string $end: regular expression specifying the end line
MoveTo (line 1225)

Moves the file to new location.

void MoveTo (string $destination)
  • string $destination: destination location
PrependLines (line 1071)

Prepends lines with a string (usually a comment character).

Any line matching the search string will be changed.

  • return: true if any matches were made
  • throws: FileNotFoundException Exception (inherited from GetContentsAsArray)
boolean PrependLines (string $search, string $prepend)
  • string $prepend: prepend string
  • string $search: regular expression used to match removed lines
Replace (line 851)

Replaces the contents of the given tempfile to this file.

This is basically a "mv" with the following behavior:

  • This file (the one passed to the constructor) must exist.
  • The tempfile is deleted if successful.
  • The tempfile will take on the same file permissions and ownership as the target file.
  • throws: FileNotFoundException FileException
void Replace (string $tempfile)
  • string $tempfile: temp file
ReplaceLines (line 1332)

Replaces a line (defined by a regular expression) with a replacement.

  • return: number of replacements made
  • throws: FileException, ValidationException
integer ReplaceLines (string $search, string $replacement, [integer $maxreplaced = -1])
  • string $search: search string
  • string $replacement: replacement line (or lines)
  • integer $maxreplaced: maximum number of matches to make
ReplaceLinesBetween (line 1260)

Replaces lines in a section of a file for Apache-style configuration files.

Specify the (non-unique) start and end tags along with a search value that uniquely defines the section.

  • throws: FileNotFoundException Exception (inherited from GetContentsAsArray)
void ReplaceLinesBetween (string $search, string $replacement, string $start, string $end)
  • string $start: regular expression specifying the start line
  • string $end: regular expression specifying the end line
  • string $search: regular expression for the search string
  • string $replacement: replacement line
ReplaceLinesByPattern (line 1452)

Replaces all matching lines defined by a regular expression.

  • return: true if any replacements were made
  • throws: FileNotFoundException Exception (inherited from GetContentsAsArray)
boolean ReplaceLinesByPattern (string $search, string $replacement)
  • string $search: search expression
  • string $replacement: replacement expression
ReplaceOneLine (line 1382)

Replaces a line defined by a regular expression.

  • return: number of replacements made
  • throws: FileNotFoundException, FileException
integer ReplaceOneLine (string $search, string $replacement)
  • string $search: search string
  • string $replacement: replacement line (or lines)
ReplaceOneLineByPattern (line 1403)

Replaces a line defined by a regular expression.

This version differs from ReplaceOneLine in that it uses preg_replace to do the substitution. Thus you can use parts of a pattern match in the replacement (ie: $1, $2, etc).

  • return: true if any replacements were made
  • throws: FileNotFoundException Exception (inherited from GetContentsAsArray)
boolean ReplaceOneLineByPattern (string $search, string $replacement)
  • string $search: search expression
  • string $replacement: replacement expression

Inherited Methods

Inherited From Engine

Engine::__construct()
Engine::AddValidationError()
Engine::CheckValidationErrors()
Engine::CopyValidationErrors()
Engine::GetValidationErrors()
Engine::Log()
Engine::__destruct()
Class Constants
CMD_CAT = '/bin/cat' (line 246)
CMD_CHMOD = '/bin/chmod' (line 251)
CMD_CHOWN = '/bin/chown' (line 250)
CMD_COPY = '/bin/cp' (line 248)
CMD_FILE = '/usr/bin/file' (line 254)
CMD_HEAD = '/usr/bin/head' (line 255)
CMD_LS = '/bin/ls' (line 252)
CMD_MD5 = '/usr/bin/md5sum' (line 253)
CMD_MOVE = '/bin/mv' (line 247)
CMD_REPLACE = '/usr/sbin/app-rename' (line 256)
CMD_RM = '/bin/rm' (line 245)
CMD_TOUCH = '/bin/touch' (line 249)

Inherited Constants

Inherited from Engine

Engine::COMMAND_API

Documentation generated on Fri, 28 Aug 2009 20:32:04 -0400 by phpDocumentor 1.3.2